PHP Sessions
A session is a global variable stored on the server.
As we know HTTP is a stateless protocol, when a user requests one page, followed by another,
HTTP does not provide a way for you to tell that both requests came from the same user.
The idea of session control is to be able to track a user during a single session on a website.
It helps us in storing the user information to be used across multiple pages.
If you can do this, you can easily support logging in a user and showing content according to her authorization level
or personal preferences.
You can track the user's behavior, and you can implement shopping cards.
Starting a Session
In PHP, the session_start() function is used to create a client session and generate a session id. Once the session has been created, we can create any number of session variables. The session variable is created in key-value pairs.
Examples
<?php
session_start();
$_SESSION['userid'] = '';
$_SESSION['department'] = '';
?>
The session_start() takes no argument and causes PHP either to notice a session id that has been passed to it or create a new session id if not found. In PHP, $_SESSION is an associative array that contains all session variables.
PHP Unset Session Variable
The session_unset() function is used to free all variables in the session. It takes no arguments.Examples
<?php
// remove all session variables
session_unset();
?>
Destroy a PHP Session
The session_destory() function is used to get rid of all session variables information that has been stored. It does not unset any variables in the current script or the session cookie.Examples
<?php
//destroy entire session
session_destroy();
?>
We can use the unset() function, if you want to destroy only a session single item.
<?php
//destroy only department session
unset($_SESSION['department']);
?>