PHP Sessions

Sessions are used to compensate with the stateless condition of the HTTP protocol. Without going into the technical details, a session allows storage of information that is associated with the client for the duration of the client's visit. A session allows the storage of variables, but not objects. There is a unique identification string for each session. Here's how to implement a session using PHP.

Place the following code at the very top of the HTML file, which should now be a PHP file with a .php file extension:

<?php
session_start();
?>

This code is required for every file in which the session may be continued. It must be placed before any HTML tag, and even before any document type declaration such as:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

Therefore if PHP and HTML are mixed, the file may start like:

<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
.
.

Adding Variables to the Session

To add a variable to a session code similar to the following may be used:

session_register("location1");
$location1="database";

If the value if $location1 is printed in another file or location during the session, the value of the variable, "database" is printed.

echo $location1, "<BR>";