PHP Session is a mechanism to store information across multiple pages. Gain understanding on how to start, modify, & destroy PHP sessions using programming examples:
In this tutorial, you will learn what a PHP session is, how to start, modify, & destroy PHP sessions. Also, see the differences between cookies and sessions, and frequently asked questions (FAQs) related to this topic.
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Series of Simple PHP Tutorials
Table of Contents:
PHP Sessions: The Ultimate Guide

What is a PHP Session
A PHP session is a technique to store and access information across multiple pages. Unlike cookies, sessions are stored on the server.
Sessions store information in variables, which are called PHP session variables. A normal variable can only be used within the page it was created. But a session variable can be created on one page and used on another.
Sessions are widely used on e-commerce websites where you need to store and pass cart details like product id, product name, product price, etc. from one page to another.
Uses of Sessions
Sessions are very useful in many ways. Some of the uses of sessions are:
- Track user activities.
- Disable multiple logins with the same user account.
- Limit the number of logins.
- Logouts a user automatically after a long period of inactivity.
How to Start a Session
A session can start anywhere on the page. The PHP session_start() function is used to start a session. The isset() function is used to check whether or not the session variable is set.
The superglobal, $_SESSION is used to store current PHP session variables.
The below example shows how to start a session.:
<?php
session_start();
if(isset( $_SESSION['visits'])){
$_SESSION['visits'] += 1;
}else{
$_SESSION['visits'] = 1;
}
echo "No. of visits in this session: ". $_SESSION['visits'];
?>
The following text shows the browser output of the above programming code:
- When you visit the web page for the first time:
No. of visits in this session: 1
- When you visit the web page for the second time:
No. of visits in this session: 2
Modifying a Session
Modifying a session is straightforward. You just need to overwrite the session variable.
Destroying a Session
In PHP, you have options to destroy either all session variables or a single session variable. The session_destroy() function destroys all session variables, while the unset() function unsets a single session variable.
- Destroy all session variables
<?php
session_destroy();
?>
- Unset a single session variable
<?php
unset($_SESSION['user']);
?>
The below example shows how to destroy a session:
<?php
session_start();
if(isset( $_SESSION['visits'])){
$_SESSION['visits'] += 1;
}else{
$_SESSION['visits'] = 1;
}
echo "No. of visits in this session: ". $_SESSION['visits'];
unset($_SESSION['visits']);
?>
The following text shows the browser output of the above programming code:
- When you visit the web page for the first time:
No. of visits in this session: 1
- When you visit the web page for the second time, third time…:
No. of visits in this session: 1
Cookies vs Sessions
The following table shows the differences between cookies and sessions:
| Parameter | Cookies | Sessions | |
|---|---|---|---|
| 1 | Storage | Store on the client-side or the web browser. | Store on the server-side. |
| 2 | Storage limit | Only store a limited amount of data (maximum 4 KB). | Can store a large amount of data. |
| 3 | Multiple variables | Do not hold multiple variables. | Holds multiple variables. |
| 4 | Reliability | Comparatively less reliable. | Comparatively more reliable. |
| 5 | Security | Less secure. | More secure. |
Frequently Asked Questions
1. What are sessions in PHP?
Sessions are a method of accessing and storing information across multiple pages.
2. Why do we need sessions in PHP?
Sessions are needed to store and pass information from one page to another. For example, in an e-commerce website, sessions are used to store and pass cart details like product id, product name, product price, etc. from one page to another.
3. Is a PHP session safe?
Yes, sessions are more secure than cookies.
4. Where are sessions stored in PHP?
Sessions are stored on the server side.
5. How do I start a PHP session?
You can start a session by using the PHP session_start() function.
6. What are the differences between cookies and sessions?
The differences between cookies and sessions are listed below:
Cookies are stored on the client-side, whereas Sessions are stored on the server-side.
Cookies can only store a maximum of 4 KB of data. Sessions can store a large amount of data.
Cookies do not hold multiple variables. Sessions hold multiple variables.
Cookies are less reliable than sessions.
Cookies are less secure than sessions.
Conclusion
A session is a technique to store and access information across multiple pages. Unlike cookies, sessions are stored on the server. You can start, modify, and destroy PHP sessions.
The PHP session_start() function is used to start a session, while the session_destroy() and unset() functions are used to destroy sessions. To modify a session variable, you just need to overwrite the session variable.






