Register one or more global variables with the current session
(PHP 4, PHP 5)
Related Examples ( Source code ) » session_register
Code Examples / Notes » session_register
You don't need to serialize an object before adding it to a session var
$obj = new exampel ();
and to resume the object:
$obj = $_SESSION['obj']);
sometimes you know everything was right but you still get an error Cannot send session cache limiter - headers already sent (output started at blah blah...; you may spend an hour until you discover that your php tag was at the second line of your script. this is true specially when you are under pressure..i hope this will help...
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.
$_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.
Make sure you put session_start() at the beggining of your script.
My sessions kept unsetting and I finally figured out why.
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.
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.
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['14'] = "blah";
Array (  => "blah" )
But if on another page (with same session) you try
$_SESSION 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.
I wasted about two days trying to figure out why some of my session data was being lost. The problem occurred intermittently and I was nearly convinced it had to be a PHP bug.
Since my session data was being stored in mysql, I finally thought to check the definition of the session table. The session data was stored in a column of type 'text' (which can only hold 65,535 bytes of data).
Changing the session column to type 'mediumtext' (which can hold 16,777,215 bytes) solved the problem.
Perhaps this tip will help someone else who may be experiencing similar session loss problems.
Do not even think of using "global" together with "$_SESSION":
// Will only work on some servers (or some PHP-Versions - I don't know):
// correct way:
took me hours to recognize :/