0
0
PHPprogramming~3 mins

Why Starting and using sessions in PHP? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your website could remember every visitor like a friendly shop assistant without asking them to repeat themselves?

The Scenario

Imagine you run a website where users log in to see their personal info. Without sessions, every time they click a new page, the site forgets who they are, and they have to log in again.

The Problem

Trying to remember users manually means passing their info in every link or form. This is slow, messy, and easy to break. It also risks exposing private data in URLs or forms.

The Solution

Sessions let the website remember each user automatically. Once a user logs in, the server keeps their info safely and links it to their browser. This way, users stay logged in as they browse.

Before vs After
Before
$user = $_GET['user']; // Pass user in URL
if ($user == 'admin') { echo 'Welcome admin'; }
After
<?php
session_start();
$_SESSION['user'] = 'admin';
echo 'Welcome ' . $_SESSION['user'];
?>
What It Enables

Sessions make it easy to create personalized, secure, and smooth user experiences across multiple pages.

Real Life Example

When you shop online, sessions remember your cart items as you browse different pages, so you don't lose your selections.

Key Takeaways

Manual tracking of users is unreliable and insecure.

Sessions store user info safely on the server linked to their browser.

This keeps users logged in and personalizes their experience.