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 — 現在のセッションに1つ以上の変数を登録する
説明
session_register() の引数の数は可変であり、 各引数は変数名を保持する文字列または変数名からなる配列 とすることが可能です。 各変数名が処理される毎に、session_register() は、その変数名のグローバル変数を現在のセッションに登録します。
配列 $_SESSION または $HTTP_SESSION_VARS (PHP < 4.1.0) の適当なメンバに設定をするだけでもセッション変数を作成することが可能です。
<?php
// session_register() の使用は推奨されません。
$barney = "A big purple dinosaur.";
session_register("barney");
// PHP 4.1.0以降では$_SESSIONの使用が推奨されます。
$_SESSION["zim"] = "An invader from another planet.";
// 古い手法としては $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() を使用している場合には、それは register_globals を不可としている環境下では動作しません。
注意: register_globals: 重要な注意
PHP 4.2.0 以降、PHP ディレクティブ register_globals のデフォルト値は off となっています。 また、このディレクティブは PHP 6.0.0 で完全に削除される予定です。 PHP コミュニティは、ユーザがこのディレクティブの設定に依存せず、 superglobals のような他の手段を使用することを推奨します。
この関数は、global変数を登録します。 セッション変数を関数の内部で登録したい場合、 global キーワードあるいは $GLOBALS[] 配列を用いてその変数をグローバルとするか、下記のように session 配列を使用してください。
$_SESSION (あるいは $HTTP_SESSION_VARS) を使用する場合は session_register()、 session_is_registered() および session_unregister() を使用しないでください。
注意: セッションにリソース変数を登録することは現在できません。例えば、 データベースへの接続を生成し、接続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.
