The PHP Session is a server-side tool used to store and retrieve information about a user or a visitor. A session must be started at the beginning of every PHP script with the session_start() function. Once started, the session will generate a unique session ID for each user, which is stored as a cookie on the visitor's browser.
A common use of the PHP Session is to keep the user logged in on the website during the same browser session. When the user logs in, their username or email is stored in a session variable, which can be accessed on every page of the website where the session is started. The session variable can also be unset or destroyed when the user logs out.
Example 1: Setting session variable
// Start the session session_start();
// Set session variable $_SESSION['username'] = 'JohnDoe'; ?>
Package library: PHP Standard Library
Example 2: Retrieving session variable
// Start the session session_start();
// Get session variable $username = $_SESSION['username'];
// Output the value echo "Welcome back, $username!"; ?>
Package library: PHP Standard Library
Example 3: Destroying session
// Start the session session_start();
// Remove all session variables session_unset();
// Destroy the session session_destroy(); ?>
Package library: PHP Standard Library
PHP Session::all - 30 examples found. These are the top rated real world PHP examples of Session::all from package huge extracted from open source projects. You can rate examples to help us improve the quality of examples.