Team LiB
Previous Section Next Section

Understanding Session Variables

In the temporary session file on the web server, session variables (and their values) are stored. Because these values and variables are not kept in a database, no additional system resources are required to connect to and extract information from database tables. You can access session variables through the $_SESSION superglobal.

For example, a temporary session file might contain the following:

count|s:7:"76";
valid|s:7:"yes";

In this example, count and valid are the names of the session variables, and 76 and yes are their respective values. However, to out the variable in the session, you must first explicitly add it to the $_SESSION superglobal. Once it is added, you can extract the value (using $_SESSION[count] or $_SESSION[valid], in this example).

When you attempt to retrieve a session variable, the sequence goes something like this (say you're trying to get the value of $_SESSION[count]):

  1. The PHP parser gets the value of $_COOKIE[PHPSESSID] from the user cookie.

  2. The PHP parser finds a matching temporary session file.

  3. Inside the session file, the PHP parser looks for count and then finds its value (say, 76).

  4. $_SESSION[count] is equal to 76.

Next, you start your own per-user counter script using a session.

Starting a Session

Starting a session is a snap. You just call the session_start() function, and PHP takes care of the rest, sending the cookie and creating the temporary file.

  1. Open a new file in your text editor and start a PHP block, and then call the session_start() function:

    <?
    session_start();
    
    
    Note 

    The session_start() function actually performs several important tasks. First, it determines whether a session has been started for the current user, and it starts one if necessary. It also alerts the PHP engine that session variables and other session-related functions will be used within the specific script.

    Because of the dual purpose of session_start(), use it at the beginning of all session-related scripts.

  2. Create a string to hold a message, and then close the PHP block:

    $msg = "started a session....";
    ?>
    
  3. Type the following HTML:

    <HTML>
    <HEAD>
    <TITLE>Start a Session</TITLE>
    </HEAD>
    <BODY>
    
  4. Display the message string:

    <? echo "$msg"; ?>
    
  5. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
  6. Save the file with the name session.php, and place this file in the document root of your web server.

  7. Open your web browser and type http://127.0.0.1/session.php.

Click To expand

If you still have your preferences set to warn before accepting cookies, you'll see a dialog box like this figure (or one appropriate to your browser).

After you click on Allow, the message will be displayed.

Click To expand

How inspiring was that? In the next section, you register an actual value and watch it change during the course of your session.

Registering and Modifying Session Variables

The goal of this script is to register a variable and change its value during the course of a user session.

  1. Open a new file in your text editor, start a PHP block, and call the session_start() function:

    <?
    session_start();
    
  2. Register a variable called count:

    session_register('count');
    
    
    Note 

    Now, for as long as this session exists, a variable called $_SESSION[count] will be available. Currently, the variable has no value.

  3. Increment the value of $_SESSION[count] to account for the current access:

    $_SESSION[count]++;
    
  4. Create a string to hold a message, including the value of $_SESSION[count]:

    $msg = "<P>You've been here $_SESSION[count] times. Thanks!</p>";
    
  5. Close the PHP block and type the following HTML:

    ?>
    <HTML>
    <HEAD>
    <TITLE>Count Me!</TITLE>
    </HEAD>
    <BODY>
    
  6. Display the message string:

    <? echo "$msg"; ?>
    
    
  7. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
  8. Save the file with the name countme.php, and place this file in the document root of your web server.

  9. Open your web browser and type http://127.0.0.1/countme.php.

Unless you closed your web browser between the last script and now, your old session will still be active and you won't see the cookie approval dialog box. You should just see the following figure.

Click To expand

Reload the page several times, and watch how the counter increments by one after each reload. For example, I reloaded the page eight times and finally saw the figure shown on the next page.

Click To expand

In the next section, you handle more than just an access count— you set and display user preferences during a user session.


Team LiB
Previous Section Next Section