Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Session Handling Functions : session_register

session_register

Register one or more global variables with the current session (PHP 4, PHP 5)
bool session_register ( mixed name [, mixed ...] )

session_register() accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays. For each name, session_register() registers the global variable with that name in the current session.

You can also create a session variable by simply setting the appropriate member of the $_SESSION or $HTTP_SESSION_VARS (PHP < 4.1.0) array.

<?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.";
?>

If session_start() was not called before this function is called, an implicit call to session_start() with no parameters will be made. $_SESSION does not mimic this behavior and requires session_start() before use.

Parameters

name

A string holding the name of a variable or an array consisting of variable names or other arrays.

...

Return Values

Returns TRUE on success or FALSE on failure.

Notes

Caution:

If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

register_globals: important note:

Since PHP 4.2.0, the default value for the PHP directive register_globals is off, and it is completely removed as of PHP 6.0.0. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.

Caution:

This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.

Caution:

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().

Note:

It is currently impossible to register resource variables in a session. For example, you cannot create a connection to a database and store the connection id as a session variable and expect the connection to still be valid the next time the session is restored. PHP functions that return a resource are identified by having a return type of resource in their function definition. A list of functions that return resources are available in the resource types appendix.

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, assign values to $_SESSION. For example: $_SESSION['var'] = 'ABC';

Related Examples ( Source code ) » session_register








Code Examples / Notes » session_register

gianni_t76

You don't need to serialize an object before adding it to a session var
example:
include("class.exmaple.php");
$obj = new exampel ();
$_SESSION['obj']=$obj;
and to resume the object:
include("class.example.php");
$obj = $_SESSION['obj']);


creepers

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...

martijn

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


guideng

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.


baldanders

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.

mikej

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.


hairguy

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.


dominik-ruess

Do not even think of using "global" together with "$_SESSION":
<?php
// Will only work on some servers (or some PHP-Versions - I don't know):
function test()
{
     global $_SESSION;
     $_SESSION["test"]=1;
}
// correct way:
function test()
{
     $_SESSION["test"]=1;
}
?>
took me hours to recognize :/


Change Language


Follow Navioo On Twitter
session_cache_expire
session_cache_limiter
session_commit
session_decode
session_destroy
session_encode
session_get_cookie_params
session_id
session_is_registered
session_module_name
session_name
session_regenerate_id
session_register
session_save_path
session_set_cookie_params
session_set_save_handler
session_start
session_unregister
session_unset
session_write_close
eXTReMe Tracker