PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users' computer. Session variables hold information about one single user and are available to all pages in one application.
Starting a PHP Session:
A session is started with the session_start()
function. This function must be the very first thing in your document, before any HTML tags.
<?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> ...
Storing Session Data:
Session variables are set with the PHP global variable: $_SESSION
.
<?php // Start the session session_start(); // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?>
Accessing Session Data:
To access session variables, you just need to refer to the $_SESSION
variable.
<?php // Start the session session_start(); // Echo session variables that were set on previous page echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>"; echo "Favorite animal is " . $_SESSION["favanimal"] . "."; ?>
Destroying a PHP Session:
To remove all global session variables and destroy the session, use session_unset()
and session_destroy()
.
<?php // Start the session session_start(); // Remove all session variables session_unset(); // Destroy the session session_destroy(); ?>
In this case, session_unset()
removes all session variables and session_destroy()
destroys the session. After this code is executed, the session will no longer exist, and any further attempts to access session variables will fail until a new session is started.
Note:
Session data is not meant to be permanent storage. The server may delete the session data if it needs to reclaim disk space or if the session has been idle for too long. Always have a backup method to access the data in case the session data is deleted.
PHP session variables example:
// File: start_session.php session_start(); // Set session variables $_SESSION['username'] = 'john_doe'; $_SESSION['user_id'] = 123; // Access session variables echo "Welcome, " . $_SESSION['username']; // File: another_page.php session_start(); // Access session variables across pages echo "User ID: " . $_SESSION['user_id'];
session_start()
function in PHP:
session_start()
at the beginning of each script to start or resume a session.// File: index.php session_start(); // Perform operations with session variables
Manage sessions in PHP:
$_SESSION
superglobal to manage and store session variables.session_start(); // Set session variable $_SESSION['language'] = 'English'; // Access session variable echo "Current language: " . $_SESSION['language'];
PHP session timeout configuration:
session.gc_maxlifetime
in php.ini
or using ini_set()
.// File: start_session.php ini_set('session.gc_maxlifetime', 3600); // 1 hour session_start(); // Perform operations with session variables
session_destroy()
in PHP:
session_destroy()
to end the current session and clear session data.// File: logout.php session_start(); session_destroy();
PHP session handling for login/logout:
// File: login.php session_start(); // Authenticate user if ($authenticated) { $_SESSION['user_id'] = $user_id; $_SESSION['username'] = $username; } // File: logout.php session_start(); session_destroy();
Cookies vs sessions in PHP:
// Using cookies setcookie('username', 'john_doe', time() + 3600, '/'); // Accessing cookie value echo "Welcome, " . $_COOKIE['username']; // Using sessions session_start(); $_SESSION['username'] = 'john_doe'; // Accessing session value echo "Welcome, " . $_SESSION['username'];