PHP sessions let your site remember a visitor from one page to the next. HTTP forgets everything between requests, without sessions, a user who logs in on one page is a stranger again on the next.
Before going any further, to understand sessions I want All My Viewers to Know that every tutorial I have Posted so far was on XAMPP which is a Web Server for your personal computer/laptops it can behave like a web server online too if you do port forwarding but anyways that's another matter let's get back to sessions.
Why PHP sessions exist
Every request to your server arrives with no memory of the last one. That is not a flaw, it is how the web was designed. It is also why a shopping cart, a logged-in user or a "welcome back" message needs something extra.
Now PHP has a function named as "session_start()."Β and here is what actually happens the moment you call session_start().
PHP creates a file on your server. On XAMPP you will find it sitting in C:\xampp\tmp with a name like sess_8f3a1c9d4b2e. Everything you put into $_SESSION gets written into that file.
Then PHP sends the browser one small thing: the name of that file. The browser stores it as a cookie called PHPSESSID.
On the next request the browser sends that cookie back. PHP reads the ID, finds the matching file, and loads it into $_SESSION. That is the whole mechanism β no magic in it.
So sessions and cookies are not two competing options. They work together:
- The session is the file on the server, holding your data.
- The cookie is the browserβs copy of that fileβs name.
That is exactly why session data is safer than cookie data. A visitor can open their browser tools and read the cookie β but all they find is an ID. The values themselves never leave your server.
One caveat so this stays honest: files are just PHPβs default storage. Larger sites move sessions into Redis or a database instead. Same idea, different shelf.
Starting a session and storing something
<?php session_start(); $_SESSION["username"] = "Ayesha"; $_SESSION["role"] = "student"; echo "Saved to the session."; ?>
Output:

session_start() must run before any output β no HTML, no echo, not even a blank line before <?php. It sends a cookie header, and headers cannot go out after content has started.
After that $_SESSION behaves like any associative array from PHP arrays. The only difference is that it survives to the next page.
Reading it on another page
<?php
session_start();
if (isset($_SESSION["username"])) {
echo "Welcome back, " . htmlspecialchars($_SESSION["username"]) . "<br>";
echo "Role: " . htmlspecialchars($_SESSION["role"]);
} else {
echo "No session found. Visit the first page.";
}
?>
Output:

A different file, and the values are still there. That is the whole point of sessions.
session_start() is needed on every page that touches $_SESSION, not just the first. Forgetting it is the most common reason a session "stops working".
Note htmlspecialchars() again β same rule as PHP forms. Session data often came from a user in the first place, so escape it on the way out.
A tiny login gate
This is the pattern behind almost every members area you have used.
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$user = trim($_POST["user"] ?? "");
$pass = trim($_POST["pass"] ?? "");
if ($user === "ayesha" && $pass === "secret123") {
session_regenerate_id(true);
$_SESSION["username"] = $user;
echo "Logged in as " . htmlspecialchars($user);
} else {
echo "Wrong details.";
}
}
?>
User:
Pass:
Log in
Output:

session_regenerate_id(true) is there on purpose. It issues a fresh session ID the moment someone logs in, so a ticket handed out before login cannot be reused afterwards. One line, and it closes a real hole called session fixation.


Loading comments...