Page 176 - Open Soource Technologies 304.indd
P. 176
Unit 10: Cookies
12: ?> Notes
13: </body>
14: </html>
Figure 10.2 shows the output from example:3. As you can see, we have access to the
$_SESSION[product1] and $_SESSION[product2] variables in an entirely new page.
Figure 10.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,
echo session_save_path();
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 example1, 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 later—as you have already
LOVELY PROFESSIONAL UNIVERSITY 171