Kritim Yantra
May 18, 2025
When building any web application, one of the most important features is maintaining user state across multiple pages — whether it’s a shopping cart, a login session, or user preferences. This is where PHP Sessions shine.
In this blog, you’ll learn everything about PHP sessions:
Let’s dive in! 🏊
A session is a way to store information (variables) across multiple pages. Unlike cookies that are stored on the user's browser, session data is stored on the server, making it more secure.
Before using any session variables, you must start a session using session_start()
— this should be at the very top of your script, before any HTML.
<?php
session_start(); // Start the session
$_SESSION['username'] = 'john_doe'; // Store data
echo 'Session started';
?>
If you forget session_start()
, the session won’t work!
Use the $_SESSION
superglobal array:
<?php
session_start();
$_SESSION['email'] = 'john@example.com';
$_SESSION['is_logged_in'] = true;
echo 'Session data saved.';
?>
<?php
session_start();
if ($_SESSION['is_logged_in']) {
echo "Welcome, " . $_SESSION['email'];
} else {
echo "Please log in.";
}
?>
You can clear session variables or destroy the session completely.
unset($_SESSION['email']);
session_start();
session_unset(); // Remove all session variables
session_destroy(); // Destroy the session itself
Sessions are powerful, but they must be handled securely to prevent hijacking:
session_regenerate_id(true);
This prevents session fixation attacks.
In production, always use SSL so session cookies are transmitted securely.
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
]);
<?php
session_start();
$user = 'admin';
$pass = '1234';
if ($_POST['username'] === $user && $_POST['password'] === $pass) {
$_SESSION['user'] = $user;
header("Location: dashboard.php");
} else {
echo "Invalid credentials";
}
?>
<?php
session_start();
if (!isset($_SESSION['user'])) {
header("Location: login.php");
exit();
}
echo "Welcome, " . $_SESSION['user'];
?>
<a href="logout.php">Logout</a>
<?php
session_start();
session_destroy();
header("Location: login.php");
?>
Function | Description |
---|---|
session_start() |
Starts/resumes a session |
$_SESSION |
Superglobal to store session data |
session_destroy() |
Destroys the session |
session_unset() |
Clears all session variables |
session_id() |
Returns the current session ID |
session_regenerate_id() |
Regenerates the session ID for security |
session_start()
.Feature | Session | Cookie |
---|---|---|
Storage | Server | Client (browser) |
Size limit | Larger | ~4KB |
Security | More secure | Less secure |
Expiry | Until browser closes or set | You can set manually |
PHP sessions are a foundational building block for creating secure and interactive web applications. Whether you’re building a login system, a cart, or user preferences — sessions make state management easy and secure.
Use them wisely, and always keep security and best practices in mind.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google