session_register

(PHP 4, PHP 5)

session_register --  Register one or more global variables with the current session

Description

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.

注意

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 重要说明: 自 PHP 4.2.0 起,PHP 中的选项 register_globals 的默认值被设为 off。PHP 社区鼓励大家不要依赖于这个选项,用其它方法替代,例如superglobals

注意

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.

注意

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

This function returns TRUE when all of the variables are successfully registered with the session.

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.

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

注: 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';

See also session_is_registered(), session_unregister(), and $_SESSION.


add a note add a note User Contributed Notes
estereofonico at gmail dot com
26-Oct-2006 05:31
This tip can be helpful to deal with numerous values in a registered variable.

<?php
session_start
();
if ( isset(
$_GET['cat']) ) { $_SESSION['cat'] = $_GET['cat'];
}
else {
$_SESSION['cat']=0;
}
?>
<a href="index.php?cat=1">et cat to 1</a>
<a href="index.php?cat=2">et cat to 2</a>
Cat is : <?php echo $_SESSION['cat']; ?>
guideng at unlv dot nevada dot edu
02-Jun-2006 04: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.
martijn at brothersinart dot net
12-Apr-2006 05: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
hairguy
21-Mar-2006 02:53
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.
creepers at businessrealmz dot com
28-Feb-2006 01:49
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...
mikej
21-Nov-2004 05: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.
baldanders
13-Nov-2004 03: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.
dominik-ruess at gmx dot de
14-Dec-2003 06:58
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 :/
gianni_t76 at yahoo dot it
05-Aug-2003 03:42
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']);
ice at thesurf dot no-ip dot com
02-Apr-2003 08:09
If you use objects it is alsways a good idear to make a var or array that hold your object-handler

exampel:
include("class.exmaple.php");
$obj = new exampel ();

$se_obj = serialize($obj);

$_SESSION['se_obj']=$se_obj;

when you then got on the next page and so a start_session()
and you don't habe includet the classdescription, your object will still not break.
Just do a
include("class.example.php");
$obj = unserialze($_SESSION['se_obj']);

and all is fine.
This is a preaty good trick if you do some includings in the next page an use the object in a file you include :)