O'Reilly logo

Sams Teach Yourself PHP in 24 Hours, Third Edition by Matt Zandstra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required