Tutorials › PHP Tutorial › PHP Sessions and Cookies

PHP Sessions and Cookies

16 min read · Lesson 20 of 24

HTTP, the language browsers use to talk to servers, has no memory. Each request arrives on its own, and the server has no idea that the visitor who just logged in is the same one now opening the dashboard. Sessions and cookies solve this by letting your site remember a visitor between pages.

Cookies vs sessions

CookieSession
Storedin the visitor's browseron the server
Visitor can read/change ityesno (only a random ID is in the browser)
Sizesmall (about 4 KB)as large as you need
Lifetimeuntil its expiry dateuntil the browser closes or it times out
Good forpreferences: theme, language, "remember me"login state, shopping cart, form messages

In fact, sessions use a cookie behind the scenes: PHP gives the browser a cookie named PHPSESSID holding a long random ID, and keeps the actual data in a file on the server under that ID.

Starting a session

Call session_start() at the top of every page that needs the session, before any HTML or echo. After that, the $_SESSION superglobal works like a normal array whose contents survive from page to page.

login.php (after the password has been checked):

Example PHP
<?php
session_start();

// In a real app these values come from the database
// after password_verify() succeeds.
session_regenerate_id(true);
$_SESSION["user_id"] = 17;
$_SESSION["name"] = "Wanjiku";
$_SESSION["role"] = "student";

echo "Login successful. Go to your dashboard.";
Result
Login successful. Go to your dashboard.
Open in editor
Warning: session_start(), setcookie() and header() all send HTTP headers, and headers must go out before any page content. If even a blank line or space is output first, you get the error "Cannot modify header information - headers already sent". Keep these calls at the very top of the file, and leave out the closing ?> in PHP-only files.

session_regenerate_id(true) gives the visitor a fresh session ID at login. This blocks an attack called session fixation, where someone plants a known session ID in a victim's browser before they log in.

Reading session data on another page

dashboard.php checks whether the visitor is logged in. If not, it sends them back to the login page. The output below is what Wanjiku sees after logging in with the code above.

Example PHP
<?php
session_start();

if (!isset($_SESSION["user_id"])) {
    header("Location: login.php");
    exit;
}

$name = htmlspecialchars($_SESSION["name"]);
echo "<h1>Karibu, $name!</h1>\n";
echo "<p>You are logged in as a {$_SESSION['role']}.</p>";
Result
<h1>Karibu, Wanjiku!</h1>
<p>You are logged in as a student.</p>
Open in editor

Always call exit right after a header("Location: ...") redirect. The header only asks the browser to go elsewhere; without exit, PHP would keep running and could still output the protected page.

A session counter

Session values can be updated like any variable. This page counts how many times the visitor has opened it during the current session. On a visitor's first visit it shows:

Example PHP
<?php
session_start();

$_SESSION["views"] = ($_SESSION["views"] ?? 0) + 1;

echo "You have opened this page {$_SESSION['views']} time(s).";
Result
You have opened this page 1 time(s).
Open in editor

Refresh the page and the number goes up: 2, 3, 4... Open the page in a private window and it starts from 1 again, because that window has a different session.

Logging out

To log out, clear the session array, delete the session cookie and destroy the session data on the server.

Example PHP
<?php
session_start();

$_SESSION = [];
setcookie(session_name(), "", time() - 3600, "/");
session_destroy();

echo "You have been logged out.";
Result
You have been logged out.
Open in editor

Setting a cookie with an expiry time in the past is how you tell the browser to delete it.

Cookies

setcookie() asks the browser to store a small value. The browser sends it back with every later request, and PHP puts it in $_COOKIE.

setcookie(name, value, options);

The most common options are:

OptionMeaning
expiresUnix timestamp when it expires (time() + 86400 * 30 = 30 days)
pathwhich part of the site receives it ("/" = the whole site)
secureonly send it over HTTPS
httponlyhide it from JavaScript (protects against XSS theft)
samesite"Lax" or "Strict" limits sending it from other sites (helps against CSRF)

This page saves the visitor's chosen theme for 30 days, then reads the cookie. The output shows a returning visitor whose browser sent theme=dark.

Example PHP
<?php
if (isset($_GET["theme"])) {
    setcookie("theme", $_GET["theme"], [
        "expires" => time() + 60 * 60 * 24 * 30,
        "path" => "/",
        "httponly" => true,
        "samesite" => "Lax",
    ]);
}

$theme = $_COOKIE["theme"] ?? "light";
$theme = in_array($theme, ["light", "dark"], true) ? $theme : "light";

echo "<body class=\"theme-$theme\">";
Result
<body class="theme-dark">
Open in editor
Note: A new cookie is not in $_COOKIE until the next request, because $_COOKIE holds what the browser sent with the current request. If you need the value immediately, use the variable you just stored.

Notice that the cookie value is checked against a list of allowed themes before being printed. Cookies come from the browser, so a visitor can change them to anything: treat them exactly like form input.

Security checklist

  • Never store passwords or sensitive data in cookies. Store only an ID or a preference.
  • Regenerate the session ID on login with session_regenerate_id(true).
  • Set the session cookie options in php.ini or with session_set_cookie_params(): httponly, secure (on HTTPS sites) and samesite.
  • Log users out after a period of inactivity by storing a "last activity" time in the session and checking it on each request.
  • Escape session values when you print them; they often started life as user input.
Tip: In Kenya, many people share computers in cyber cafés. Make the logout link easy to find, and do not set long-lived "remember me" cookies by default.
Exercise

Start the session before using $_SESSION.

();
$_SESSION["cart"] = [];
Exercise

Read the "lang" cookie, falling back to "en".

$lang = ["lang"] ?? "en";
Exercise

Stop the script right after redirecting.

header("Location: login.php");
;
Sign in or create a free account to track progress, earn XP, badges and a certificate.