Accessing a unique session identifier in each of your PHP documents is only the start of session functionality. When a session is started, you can store any number of variables in the $_SESSION superglobal and then access them on any session-enabled page.
Listing 12.3 adds two variables into the $_SESSION superglobal: product1 and product2 (lines 3 and 4).
Listing 12.3. Storing Variables in a Session
The magic in Listing 12.3 will not become apparent until the user moves to a new page. Listing 12.4 creates a separate PHP script that accesses the variables stored in the $_SESSION superglobal.
Listing 12.4. Accessing Stored Session Variables
Figure 12.2 shows the output from Listing 12.4. As you can see, we have access to the $_SESSION[ "product1" ] and $_SESSION[ "product2" ] variables in an entirely new page.
Figure 12.2. Accessing stored session variables.
Although not a terribly interesting or useful example, the script does show how to access stored session variables. Behind the scenes, PHP writes information to a temporary file. You can find out where this file is being written on your system by using the session_save_path() function. This function optionally accepts a path to a directory and then writes all session files to it. If you pass it no arguments, it returns a string representing the current directory to which session files are saved. On my system,
prints /tmp. A glance at my /tmp directory reveals a number of files with names like the following:
sess_fa963e3e49186764b0218e82d050de7b sess_76cae8ac1231b11afa2c69935c11dd95 sess_bb50771a769c605ab77424d59c784ea0
Opening the file that matches the session ID I was allocated when I first ran Listing 12.2, I can see how the registered variables have been stored:
product1|s:17:"Sonic Screwdriver";product2|s:8:"HAL 2000";
When a value is placed in the $_SESSION superglobal, PHP writes the variable name and value to a file. This information can be read and the variables resurrected lateras you have already seen. After you add a variable to the $_SESSION superglobal, you can still change its value at any time during the execution of your script, but the altered value won't be reflected in the global setting until you reassign the variable to the $_SESSION superglobal.
The example in Listing 12.3 demonstrates the process of adding variables to the $_SESSION superglobal. This example is not very flexible, however. Ideally, you should be able to register a varying number of values. You might want to let users pick products from a list, for example. In this case, you can use the serialize() function to store an array in your session.
Listing 12.5 creates a form that allows a user to choose multiple products. You will use the session variables to create a rudimentary shopping cart.
Listing 12.5. Adding an Array Variable to a Session Variable
We start or resume a session by calling session_start() on line 2. This call gives us access to any previously set session variables. We begin an HTML form on line 23 and, on line 25, create a SELECT element named form_products, which contains OPTION elements for a number of products.
By the Way
Remember that HTML form elements that allow multiple selections should have square brackets appended to the value of their NAME arguments. This makes the user's choices available in an array.
Within the block of PHP code beginning on line 10, we test for the presence of the $_POST["form_products"] array (line 11). If the variable is present, we can assume that the form has been submitted and information has already been stored in the $_SESSION superglobal.
We then test for an array called $_SESSION["products"] on line 12. If the array exists, it was populated on a previous visit to this script, so we merge it with the $_POST["form_products"] array, extract the unique elements, and assign the result back to the $products array (lines 1315). We then add the $products array to the $_SESSION superglobal on line 16.
Line 34 contains a link to another script, which we will use to demonstrate our access to the products the user has chosen. We create this new script in Listing 12.6, but in the meantime you can save the code in Listing 12.5 as arraysession.php.
Listing 12.6. Accessing Session Variables
Moving on to Listing 12.6, we see how to access the items stored in the session created in arraysession.php.
Once again, we use session_start() to resume the session on line 2. We test for the presence of the $_SESSION["products"] variable on line 11. If it exists, we unserialize it and loop through it on lines 1315, printing each of the user's chosen items to the browser. Figure 12.3 shows an example.
Figure 12.3. Accessing an array of session variables.
For a real shopping cart program, of course, you would keep product details in a database and test user input, rather than blindly store and present it, but Listings 12.5 and 12.6 demonstrate the ease with which you can use session functions to access array variables set in other pages.