If you want to store an object into the session, you have to check up that object can be serialized at all.
For example, if your object contains aggregated PDO object (which can't be serialized), you will get an error and no data would be stored.
session_register
(PHP 4, PHP 5)
session_register — 하나 이상의 전역 변수를 현재 세션에 등록
설명
session_register()는 변수명을 가지는 문자열이나 변수명 또는 다른 배열을 포함하는 배열을 가변 길이 인수로 받습니다. 각 이름에 대해서, session_register()는 그 이름을 가지는 전역 변수를 현재 세션에 등록합니다.
$_SESSION이나 $HTTP_SESSION_VARS(PHP < 4.1.0) 배열에 적합한 멤버를 설정해서 세션 변수를 생성할 수 있습니다.
<?php
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");
// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";
// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?>
이 함수를 호출하기 전에 session_start()를 호출하지 않으면, 암시적으로 인수 없이 session_start()를 호출합니다. $_SESSION은 이러한 동작을 하지 않으므로, 사용하기 전에 session_start()가 필요합니다.
이 함수는 PHP 5.3.0부터 배제되었고 PHP 6.0.0부터 제거됩니다. 이 기능에 의존하지 않기를 권합니다.
인수
- name
-
변수명을 가지는 문자열이나 변수명 또는 다른 배열을 포함하는 배열.
- ...
-
반환값
성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.
주의
스크립트가 register_globals에 관계 없이 작동하게 하려면, $_SESSION 배열을 사용해야 합니다. $_SESSION 엔트리는 자동적으로 등록됩니다. 스크립트가 session_register()를 사용한다면, PHP 지시어 register_globals가 꺼져 있는 환경에서는 작동하지 않습니다.
Note: register_globals: 중요 경고
PHP 4.2.0부터 PHP 지시어 register_globals의 기본값은 off이고, PHP 6.0.0에서 완전히 사라집니다. PHP 커뮤니티에서는 이 지시어에 의존하지 않는 superglobals 방식의 사용을 권합니다.
이 함수는 전역 변수를 등록합니다. 세션 변수를 함수 안에서 등록하려면, global 키워드나 $GLOBALS[] 배열을 사용해서 전역으로 만들거나, 아래에 설명된 특수 세션 배열을 사용해야 합니다.
$_SESSION(또는 $HTTP_SESSION_VARS)을 사용한다면, session_register(), session_is_registered(), session_unregister()를 사용하지 마십시오.
Note: 자원 변수를 세션에 등록할 수 없습니다. 예를 들면, 데이터베이스 접속을 생성해서 접속 id를 세션 변수에 등록한 다음에 세션이 복구되었을 때, 접속은 유효하지 않습니다. 자원을 반환하는 PHP 함수는 함수 정의에 반환형이 resource로 되어 있습니다. 자원을 반환하는 함수 목록이 자원형 부록에 있습니다.
$_SESSION(또는 PHP 4.0.6까지 $HTTP_SESSION_VARS)을 사용한다면, $_SESSION에 값을 할당하십시오. 예: $_SESSION['var'] = 'ABC';
참고
- session_is_registered() - 전역 변수가 세션에 등록되었는지 확인
- session_unregister() - 현재 세션에셔 전역 변수를 등록 해제
- $_SESSION
session_register
13-Nov-2009 09:14
14-Aug-2009 09:18
if you remove session_register() calls and replace with $_SESSION assignments, make sure that it wasn't being used in place of session_start(). If it was, you'll need to add a call to session_start() too, before you assign to $_SESSION.
26-May-2009 02:15
If you have an old code with a lot of call to the function session_register(), and you would like to make it compatible with PHP5 or PHP6, instead of rewriting all your code by replacing this function by $_SESSION['var']="val"; you could use the function set_session_vars(), that is used exactly the same way than session_register() (but there is no implicit call to session_start() ).
<?php
function set_session_vars() {
$nb_args=func_num_args();
$arg_list=func_get_args();
for($i=0;$i<$nb_args;$i++) {
global $$arg_list[$i];
$_SESSION[$arg_list[$i]] = $$arg_list[$i];
}
}
?>
19-May-2009 11:15
For those of you who use this function (session_register that is), even though the manual does specify that this function implicitly calls session_start(), I just wanted to reiterate that fact. It is also important to know that if you ever switch from session_register to using $_SESSION, make sure to call session_start before adding items to the $_SESSION variable, because unlike session_register, no implicit call to session_start is done.
Another reason I explain this is because I ran into a problem in which you can add items to the $_SESSION variable all you want, but if session_start is not called before adding them, they will not actually be saved to the session. Using the same code, though, and replacing the $_SESSION assignments with session_register without calling session_start WILL save that info to the session.
It would be nice to have PHP check for writes to the $_SESSION variable and complain with a warning if session_start hasn't been called.
01-Jun-2006 08:10
Make sure you put session_start() at the beggining of your script.
My sessions kept unsetting and I finally figured out why.
On my script, session_start() has to be said and uses cookies to set the session.
But I was outputting html prior to calling session_start(), which prevented it from succeeding becouse it uses the header function to place the cookies.
Once html has been outputed, session_start() can't use the header function to set cookies, hence session_start() fails and no session can be started.
11-Apr-2006 09:04
Please note that if you use a "|" sign in a variable name your entire session will be cleared, so the example below will clear out all the contents of your session.
<?php
session_start();
$_SESSION["foo|bar"] = "foo";
?>
It took me quite some time finding out why my session data kept disappearing. According to this bugreport this behaviour is intended.
http://bugs.php.net/bug.php?id=33786
21-Nov-2004 09:40
I've noticed that if you try to assign a value to a session variable with a numeric name, the variable will not exist in future sessions.
For example, if you do something like:
session_start();
$_SESSION['14'] = "blah";
print_r($_SESSION);
It'll display:
Array ( [14] => "blah" )
But if on another page (with same session) you try
session_start();
print_r($_SESSION);
$_SESSION[14] will no longer exist.
Maybe everyone else already knows this, but I didn't realize it until messing around with a broken script for quite a while.
12-Nov-2004 07:05
If you are using sessions and use session_register() to register objects, these objects are serialized automatically at the end of each PHP page, and are unserialized automatically on each of the following pages. This basically means that these objects can show up on any of your pages once they become part of your session.
