A PHP session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.

Example:

Let’s say you have a login page. When a user logs in, you might store their username in a session variable. Then, on each page, you can check to see if that session variable exists. If it does, the user is known to be logged in.

session_start();

// Set session variables
$_SESSION[“username”] = “John Doe”;
$_SESSION[“favcolor”] = “green”;

// Retrieve session variables
echo “Username: ” . $_SESSION[“username”];
echo “Favorite color: ” . $_SESSION[“favcolor”];

// Destroy session
session_destroy();

Leave a Reply

Your email address will not be published. Required fields are marked *