Specifications

It is generally easier to compile with --enable-trans-sid, where possible. Note also that the
SID constant will only work like this if you have configured PHP with --enable-track-vars.
Implementing Simple Sessions
The basic steps of using sessions are
Starting a session
Registering session variables
Using session variables
Deregistering variables and destroying the session
Note that these steps dont necessarily all happen in the same script, and some of them will
happen in multiple scripts. Lets talk about each of these steps in turn.
Starting a Session
Before you can use session functionality, you need to actually begin a session. There are three
ways you can do this.
The first, and simplest, is to begin a script with a call to the session_start() function:
session_start();
This function checks to see if there is already a current session ID. If not, it will create one. If
one already exists, it essentially loads the registered session variables so that you can use them.
Its a good idea to call session_start() at the start of all your scripts that use sessions.
Second, a session will be started when you try to register a session variable (see the next sec-
tion).
The third way you can begin a session is to set PHP to start one automatically when someone
comes to your site. You can do this with the
session.auto_start option in your php.ini
filewell look at this when we discuss configuration.
Registering Session Variables
In order for a variable to be tracked from one script to another, you need to register it with a
call to session_register(). For example, to register the variable $myvar, you could use the
following code
$myvar = 5;
session_register(“myvar”);
Using Session Control in PHP
C
HAPTER 20
20
USING SESSION
C
ONTROL IN
PHP
433
25 7842 CH20 3/6/01 3:42 PM Page 433