Specifications

When you are finished with a session, you should first deregister all the variables and then call
session_destroy();
to clean up the session ID.
Simple Session Example
Some of this might seem a little abstract, so lets look at an example. Well implement a set of
three pages.
On the first page, well start a session and register the variable $sess_var. The code to do this
is shown in Listing 20.1.
LISTING 20.1 page1.phpStarting a Session and Registering a Variable
<?
session_start();
session_register(“sess_var”);
$sess_var = “Hello world!”;
echo “The content of \$sess_var is $sess_var<br>”;
?>
<a href = “page2.php”>Next page</a>
We have registered the variable and set its value. The output of this script is shown in
Figure 20.1.
Using Session Control in PHP
C
HAPTER 20
20
USING SESSION
C
ONTROL IN
PHP
435
FIGURE 20.1
Initial value of the session variable shown by page1.php.
25 7842 CH20 3/6/01 3:42 PM Page 435