PHP Sessions Introduction
In this page:
session_start(); // first line, before any output
$_SESSION["key"] = value; // store
$value = $_SESSION["key"]; // read
What is a Session?
A session lets your app remember information about one visitor -- like their username or shopping cart contents -- as they move between pages, even though HTTP itself has no memory of previous requests.
Unlike cookies, the actual data is stored on the server; the browser only holds a small session ID.
Example: What is a Session?
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['username']` to "Alice"
$_SESSION['username'] = "Alice";
// Print "Only the session ID is stored in the browser's cookie" to the output
echo "Only the session ID is stored in the browser's cookie";
?>
Login to try C/C++/Java/PHP code in the editor
Storing Session Data
You store session data by writing key-value pairs directly into the $_SESSION superglobal array, for example $_SESSION[user_id] = 5. PHP automatically associates that data with the visitor's session ID behind the scenes.
Example: Storing Session Data
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Print "Stored user_id in the session" to the output
echo "Stored user_id in the session";
?>
Login to try C/C++/Java/PHP code in the editor
Retrieving Session Data
As long as session_start() runs at the top of the page, you can read back anything stored earlier in $_SESSION from any script in your app, which is what lets a login persist across an entire site.
Example: Retrieving Session Data
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Print `"Logged in as user #" . $_SESSION['user_id']` to the output
echo "Logged in as user #" . $_SESSION['user_id'];
?>
Login to try C/C++/Java/PHP code in the editor
Deleting Session Keys
Calling unset($_SESSION[key]) removes one specific piece of session data without disturbing anything else the user has stored, useful for things like clearing a single cart item.
Example: Deleting Session Keys
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['cart']` to `["item1", "item2"]`
$_SESSION['cart'] = ["item1", "item2"];
// Call `unset($_SESSION['cart'][0])`
unset($_SESSION['cart'][0]);
// Print a human-readable dump of `$_SESSION['cart']`
print_r($_SESSION['cart']);
?>
Login to try C/C++/Java/PHP code in the editor
Destroying Sessions
Logging a user out completely means clearing all session data with session_unset() and then destroying the session itself with session_destroy(), so no leftover state can be reused by the next visitor on a shared browser.
Example: Destroying Sessions
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Call `session_unset()`
session_unset();
// Call `session_destroy()`
session_destroy();
// Print `isset($_SESSION['user_id']) ? "Still logged in" : "Logged out completely"` to the output
echo isset($_SESSION['user_id']) ? "Still logged in" : "Logged out completely";
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting
session_start()before using$_SESSION. - Calling
session_start()after output has been sent, which causes a headers-already-sent warning. - Storing sensitive data in a session without securing it.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: