← Back to PHP Course | Chapter 12: Sessions & Cookies | Lesson 1 of 5

PHP Sessions Introduction

A session helps a website remember one visitor while they click around, like a coat check that keeps your ticket number. The site keeps your data and knows it is you on every page.
Syntax
php
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
<?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";
?>

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
<?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";
?>

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
<?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'];
?>

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
<?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']);
?>

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
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting session_start() before using $_SESSION.
  2. Calling session_start() after output has been sent, which causes a headers-already-sent warning.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.