PHP Sessions
PHP sessions can be used to store user information on the website and retrieve it across multiple pages. Sessions enable PHP to keep users logged in as they are navigating around the website (not as HTTP cannot remember previous requests).
As sessions keep this information on the server rather than the browser, they are suitable for more personal information, such as whether the user is logged on and their user ID.
Why PHP Sessions Are Important
PHP sessions enable websites to be dynamic and User focused. e.g. When a user logs in, the website has to remember that user.
Without sessions, the only workaround would be to ask the user to login on each page he visits, making the application really painful to use.
How PHP Sessions Work
The simple process of managing PHP sessions is as follows:
Firstly PHP generates a unique session ID.
Then, the server stores session data associated with that ID.
Then, the session ID is provided on the browser.
Finally, the ID is sent again by the browser with each request.
This procedure enables PHP to identify the same individual on another page.
Starting a Session in PHP
In order to use sessions you will need to begin the sessions at the top of the page.
<?php
session_start();
?>
This statement instructs PHP to either open a new session or resume an existing one.
Storing Data in PHP Sessions
These values can be stored in the superglobal $_SESSION.
<?php
session_start();
$_SESSION['username'] = "Varsha";
$_SESSION['role'] = "admin";
?>
PHP will store this information for the duration of the session.
Accessing Session Data
Check the same $_SESSION variable to access session data.
<?php
session_start();
echo $_SESSION['username'];
?>
This is great for dashboards and login systems.
Removing Session Data
You can remove a single session value like this:
<?php
unset($_SESSION['role']);
?>
To remove all session data and log the user out:
<?php
session_start();
session_destroy();
?>
Then the user is totally forgotten by PHP, because after destroying the session, PHP does not remember the user anymore.
Common Use Cases of PHP Sessions
PHP sessions are widely used in many real world applications. Every use case helps the website hold on to vital data while the user moves from page to page.
User Authentication
The sessions keep track if the user signed in, so the user won‘t have to log in on every page. Overall, most login systems use session to determine whether or not the user is logged in.
Admin Panels
Admin dashboard monitor sessions in order to check the login has admin permission, this avoids unauthorized people to control important settings.
Shopping Carts
Ecommerce websites utilize sessions in order to save the products that are added to the shopping cart. Once a product is added, it does not matter that the user navigates to other pages until the check out time so that the product persists.
Multi Page Forms
The balance system is split into some parts. The sessions are used to store the user input for each step so the input is not lost when the other page will be created.
User Dashboards
The personal dashboard presents data relevant for individual users such as profile data or activity history. Sessions use personal dashboard data for establishing which user is logged on, and for loading correct data.
Page Access Control
Sessions require a user to have access to protected pages. If no session exist PHP automatically redirects the user to a login page.
Temporary User Data
Sessions simply hold ‘short term’ information like ‘You have successfully signed up’ or ‘You haven‘t logged in’. These messages will only be displayed once.
Check out our resources!
- Bootstrap Templates: Explore our Bootstrap Projects section.
- Free E-Books: Download your Free E-Books here.


