- The session is a superglobal variable and it is an alternative way to store data across multiple pages or it preserve data across subsequent HTTP requests.
- The session can be defined as a method to store information on the server, which can be used in the future.
- When you work with a web server, the webserver don't know who you are, when you last work with it, what you do, etc. That means the HTTP address does not maintain the address.
- The session introduces a problem for this problem by storing information across multiple pages.
- If you set a session value to a variable, it can be used across multiple pages.
- The session variable holds the information in one variable and it can be used all the web pages of the same application until the session is destroyed.
- Session and Cookies are used to store user information.
- Sessions are stored in the server-side machine, whereas cookies are stored in a client-side machine.
- For example when a user login to the server, a session will create in the webserver.
Start a session in PHP
<?php
session_start();
?>
Create a session variable
Get the session variable
<?php
echo $_SESSION['user_name'];
?>
To show all the session variable
print_r($_SESSION);
To destroy a session
//To destroy a single variable
unset($_SESSION['user_name']);
//To remove all the session variables
session_unset();
//To destroy the session
session_destroy();