Tutorials

Learn web design and programming with our free video and text tutorials.

PHP Sessions Tutorial

PHP is a stateless protocol, which means that a server request knows nothing about another server request. This means that any information that is gathered from a visitor when they visit a web page, index.php for example, is lost when that visitor goes to another web page.

A session in PHP is a way to store and transfer user information such as a username, shopping cart items, etc., across multiple web pages on the web server. This information is kept in the form of variables and is used across multiple pages.

To create a session, use the session_start function. The call to session_start should be set before any (X)HTML code.

Every call to session_start will cause PHP to look up the session specified by an ID that is automatically assigned to each user who visits the website. If PHP does not find a session ID, PHP creates a new session and also an ID that will be associated with the session that was just created.

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebDevelopmentTutorials.com</title>
</head>
<body>

<?php
$id = session_id;
?>
<p>
The session ID is: <?php echo $id; ?>
</p>

</body>
</html>

Creating and reading session variables

Variables can be assigned to a session by using the $_SESSION global variable. Any value that is assigned to a $_SESSION variableis readable by any script that is served by your web server and is viewed in the same session.

When a session is destroyed due to inactivity or by a call to session_destroy, the data held in the $SESSION array is deleted.

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebDevelopmentTutorials.com</title>
</head>
<body>

<?php
$_SESSION["test"] = "Hello World";
$value = $_SESSION["test"];

echo "The value of the session variable "test" is ($value)";
?>

</body>
</html>

The code above will display:

Hello World

Save session data to a file

Session data remains on the web server for as long as a session remains active. When a session expires or is forcibly deleted, any data that is associated with that session is deleted.

Session data can be saved using the session_encode function.

Session data is typically saved to a database or a file system so that it can be used at a later time.

To store the data returned by session_encode in a file, functions such as fopen, fwrite and fclose can be used to create a file handle, write to a file and to close the file handle.

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebDevelopmentTutorials.com</title>
</head>
<body>

<?php
$_SESSION["firstname"] = "John";
$_SESSION["lastname"] = "Doe";

$fh = fopen("session.txt" , "w+");
$data = session_encode;

fwrite($fh, $data);
fclose($fh);
?>
<p>
The session ID is: <?php echo $id; ?>
</p>

</body>
</html>