Sessions & Cookies

HTTP is stateless — by default, a server has no memory of who made the last request. Sessions and cookies are the two mechanisms PHP gives you to bridge that gap, which is how a site remembers you're logged in from one page to the next.

Starting a session

session_start() must run before any other output — even a stray blank line before <?php can break it, since it works by sending an HTTP header:

PHP start.php
<?php
session_start();

$_SESSION["username"] = "jamie99";
$_SESSION["cart_count"] = 3;

echo "Session started for " . $_SESSION["username"];
?>
Output
Session started for jamie99

Behind the scenes, session_start() sends the visitor's browser a cookie containing a unique session ID, and stores $_SESSION's data on the server, keyed by that ID. The browser only ever holds the ID — the actual data stays server-side, which is why sessions are the right place for anything sensitive that a cookie alone shouldn't hold directly.

Reading session data on a later request

PHP dashboard.php
<?php
session_start();

if (isset($_SESSION["username"])) {
    echo "Welcome back, " . $_SESSION["username"];
} else {
    echo "Please log in.";
}
?>
Output, on a later page load, same visitor
Welcome back, jamie99

Every page that needs the logged-in visitor's data calls session_start() first, then reads $_SESSION as if it had never gone away — PHP reconnects it to the same data automatically using the session cookie the browser sends back with every request.

Ending a session: logout

PHP logout.php
<?php
session_start();
$_SESSION = [];
session_destroy();

echo "You have been logged out.";
?>
Output
You have been logged out.

Clearing $_SESSION to an empty array and calling session_destroy() removes the server-side session data entirely — any later page that checks isset($_SESSION["username"]) will find it gone.

Cookies

A cookie, unlike session data, is stored directly in the visitor's browser and sent back with every request to the same site — useful for small, non-sensitive preferences that should persist even across separate browsing sessions:

PHP theme.php
<?php
setcookie("theme", "dark", time() + (86400 * 30), "/");

$theme = $_COOKIE["theme"] ?? "light";
echo "Using theme: $theme";
?>
Output, on the request immediately after the cookie is set
Using theme: light

setcookie() takes a name, a value, an expiration time (here, 30 days from now, in seconds), and a path. Note the output still shows light on this same request — a cookie set with setcookie() only becomes readable through $_COOKIE on the next request, since it has to make a round trip to the browser first.

Note: never store anything sensitive — passwords, full credit card numbers, raw account balances — directly in a cookie. Cookies live in the visitor's browser, fully readable and editable by anyone with access to that machine. Sessions exist precisely because the data that matters should stay on the server, with only an opaque ID handed to the browser.
Course complete: that covers the PHP course from top to bottom — variables and PHP's loose typing, strings, operators (and the == versus === trap), conditionals, loops, indexed and associative arrays, functions, reading form input safely, querying a database through PDO's prepared statements, and remembering a visitor across requests with sessions and cookies. From here, the natural next steps are a real framework (Laravel or Symfony) and pairing what you've learned here with the SQL course to go deeper on the database side.