By Steve Suehring, Janet Valade

The key to using PHP sessions is the session_start() function. You call session_start() on every page and subsequently have access to all the items in the $_SESSION array.

It might seem like a bit of an odd name for the function, session_start(), because on most pages you really just want to continue the session and access the variables that are there. But in reality, session_start() does both: It starts a new session if need be and continues an existing session where appropriate.

The session_start() function is called simply like this:

<?php
session_start();
// Other PHP statements here
?>

Here’s code for a few pages that track when you accessed the first page of the application. This shows the use of the session_start() function and then creation of a variable to hold the initial access time.

Here is the code for the first page, called page1.php.

<?php
session_start();
$_SESSION['accessTime'] = date("M/d/Y g:i:sa");
print "This is page 1<br />";
print "You accessed the application at: " . $_SESSION['accessTime'];
print "<div><a href="page2.php">Continue to next page</a></div>";