Sessions are very similar to cookies in that they can be used for passing values between pages of a website. Rather than storing the values in each web browser, however, the values are stored on the web server, and a single identity cookie is used to tell PHP which set of values corresponds to the current user.

Because much less data is sent back and forth between the web server and browser, sessions are more efficient than cookies when larger amounts of data are stored.

Creating a Session

To initialize a new session in a PHP script, you use the session_start function. You can use an optional argument to specify a session name, but usually this is not required. Every script on your site that starts the same session will be able to access the same set of session variables.

The call to session_start to create a new session is as simple as the following:


The $_SESSION super-global array is used to store and retrieve session variables. Unlike the other super-globals you have encountered so far, you can assign values directly to $_SESSION, after which they are available to any script that shares the session.

Consider the script in Listing 14.1, which maintains two session variablesa count of the number of times you have viewed the page and the timestamp of the last visit.

Listing 14.1. Using Session Variables to Track Visits to a Page
 <?php session_start(); if ($_SESSION["last_visit"]) {   echo "Date of last visit: ";   echo date("j F Y, H:i:s", $_SESSION["last_visit"]);   echo "<br>";   echo "Total visits: ".$_SESSION["num_visits"]; } else   echo "This is your first visit"; $_SESSION["last_visit"] = time(); $_SESSION["num_visits"]++; ?> 

Each time the page is loaded, the old values are displayed and the new values set. Notice that if you surf to other websites and then come back, these values are remembered, but if you close your web browser and come back, the values are reset.

Using Session Variables

One of the advantages of session variables over cookies is their ability to use PHP's data types. Cookie values are always simple text values, but a session variable can take any value that a regular PHP variable can.

For instance, to store a list of items in a cookie, you would have to create an array and pass it to serialize to store. By using a session variable, you can create an array directly and store that data structure in the session.

The example in Listing 14.2 uses an array stored in the session to retain a list of values entered through a form. This is a fairly trivial example, but it demonstrates the flexibility you have when using session variables.

Listing 14.2. Using Arrays as Session Variables
 <?php session_start(); if (isset($_POST["word"]))   $_SESSION["words"][] = $_POST["word"]; if (is_array($_SESSION["words"])) {   foreach($_SESSION["words"] as $word) {     echo $word . "<br>";   } } ?> <FORM ACTION="list.php" METHOD=POST> Enter a word: <INPUT SIZE="10" NAME="word"> <INPUT TYPE=SUBMIT VALUE="Add word to list"> </FORM> 

    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: