Working with Session Variables

Accessing a unique identifier on each of your PHP documents is only the start of PHP’s session functionality. You can set any number of variables as elements of the superglobal $_SESSION array. After these are set, they are available to future requests in the session.

Listing 20.2 registers two variables with a session (lines 10 and 11).

Listing 20.2. Registering Variables with a Session
 1: <?php
 2: session_start();
 3: ?>
 4: <!DOCTYPE html PUBLIC
 5:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 6:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 7: <html>
 8: <head>
 9: <title>Listing 20.2 Registering Variables with a Session</title> 10: </head> 11: <body> 12: <div> 13: <?php 14: $_SESSION['product1'] = "Sonic Screwdriver"; ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.