PHP | Sessions (original) (raw)

Last Updated : 17 Dec, 2025

A session in PHP is a mechanism that allows data to be stored and accessed across multiple pages on a website. When a user visits a website, PHP creates a unique session ID for that user. This session ID is then stored as a cookie in the user's browser (by default) or passed via the URL. The session ID helps the server associate the data stored in the session with the user during their visit.

How Do PHP Sessions Work?

**Note: PHP sessions allow different PHP pages to share data. By calling session_start() on every page, the same session data becomes accessible across all pages, effectively connecting them during a user’s visit.

How to Use PHP Sessions?

Using PHP sessions involves several key steps: starting a session, storing data in session variables, retrieving data, and eventually destroying the session when no longer needed.

1. Starting a Session

To begin using sessions in PHP, you need to start the session with session_start() at the very beginning of the PHP script. This function ensures that the session is available and creates a unique session ID if it doesn’t already exist.

**Note: Always call session_start() before any HTML output in your PHP script. If you output HTML or whitespace before calling session_start(), it will cause an error.

2. Storing Data in Sessions

Once the session is started, you can store any information in the $_SESSION superglobal array. This allows you to carry data across different pages on the website.

The username and user ID are stored in the session for use on other pages.

3. Retrieving Session Data

Once data is stored in a session, it can be accessed on any page where the session is started.

You can use the session variables to display user-specific information, check login statuses, and perform various operations.

4. Checking if Session Variables Exist

Before using session data, it’s a good practice to check if the session variable exists to avoid errors.

5. Destroying Sessions

When a session is no longer needed, you can terminate it by using session_destroy(). This function removes all session data from the server. However, it does not automatically unset session variables; you need to manually clear them using unset() if needed.

If you want to log out the user, destroying the session will remove all user-specific data and effectively "log them out."

PHP Session Functions

PHP provides several built-in functions to work with sessions. Below are some of the most commonly used functions:

session_start(); // Start a session

$_SESSION['user_id'] = 1; // Store data
echo $_SESSION['user_id']; // Retrieve data

session_start();
session_destroy(); // Ends the session

session_regenerate_id(true); // Regenerate the session ID

Why Use PHP Sessions?

Advantages of PHP Sessions

The advantages of PHP Sessions are mentioned below:

PHP Sessions vs. Cookies

Below is the following difference between PHP Session and PHP Cookies.

Sessions Cookies
Data is stored on the server. Data is stored on the client-side (in the browser).
More secure as session data is not stored on the client-side. Less secure as data is stored on the client-side and can be changed or stolen.
Sessions usually expire when the browser is closed or after a specified inactivity time. Cookies can have an expiration date set to stay persistent across browser sessions.