Specifications

Note that you need to pass a string containing the name of the variable to
session_register(). This string should not include the $ symbol.
This will record the variable name and track its value. The variable will be tracked until the
session ends, or until you manually deregister it.
You can register more than one variable at once by providing a comma-separated list of vari-
able names; for example
session_register(“myvar1”, “myvar2”);
Using Session Variables
To bring a session variable into scope so that it can be used, you must first start a session using
one of the options described previously.
You can then access the variable. If you have
register_globals turned on (as discussed in
Chapter 1, PHP Crash Course), you might access it via its short form name; for example,
$myvar. If you dont have this turned on, you can access a variable via the associative array
$HTTP_SESSION_VARS as, for example, $HTTP_SESSION_VARS[“myvar”].
A session variable cannot be overridden by GET or POST data, which is a good security feature,
but something to bear in mind when coding.
On the other hand, you need to be careful when checking if session variables have been set
(via, say, isset() or empty()). Remember that variables can be set by the user via GET or
POST. You can check a variable to see if it is a registered session variable by calling the
session_is_registered() function. You call this function like this:
$result = session_is_registered(“myvar”);
This will check whether $myvar is a registered session variable and return true or false.
Alternatively, you can also check the $HTTP_SESSION_VARS array for a variables existence.
Deregistering Variables and Destroying the Session
When you are finished with a session variable, you can deregister it using the
session_unregister() function, as follows:
session_unregister(“myvar”);
Again, this function requires the name of the variable you want to deregister as a string, and
without the $ symbol. This function can only deregister a single session variable at a time
(unlike session_register()). You can, however, use session_unset() to deregister all the
current session variables.
Advanced PHP Techniques
P
ART IV
434
25 7842 CH20 3/6/01 3:42 PM Page 434