Session & Cookies in PHP – The Ultimate Guide to Remembering Things!

Ever walked into a room and forgot why you were there?  Well, computers have the same problem! They forget things as soon as the page refreshes. That's where sessions & cookies come to the rescue! 

Today, we’ll explore how to make PHP remember things using sessions and cookies, with real-world examples! Buckle up!

What Are Sessions & Cookies? 

Session:

  • Think of it as a temporary notebook  that PHP keeps open while a user is browsing.
  • Data is stored on the server.
  • It disappears when the user closes the browser (unless configured otherwise).

Cookie:

  • Think of it as a sticky note  that the browser holds onto.
  • Data is stored on the user’s computer.
  • It can stay for days, weeks, or even years.

Setting & Retrieving a Session

A session helps store user-specific data while they navigate your site.

Step 1: Start the Session

Before using sessions, always start with:

session_start();

Step 2: Store Data in a Session

$_SESSION['username'] = 'JohnDoe';
echo "Hello, " . $_SESSION['username'];

Now, JohnDoe’s name is remembered until they close the browser!

Step 3: Destroy a Session

To log out and clear session data:

session_destroy();

Boom! Everything is wiped out!

Setting & Retrieving a Cookie

A cookie lets you store small pieces of data on the user’s device.

Step 1: Set a Cookie

setcookie("user", "JohnDoe", time() + (86400 * 7), "/");
  • "user" = Cookie name
  • "JohnDoe" = Value stored
  • time() + (86400 * 7) = Expires in 7 days
  • "/" = Available site-wide

Step 2: Retrieve the Cookie 

if(isset($_COOKIE['user'])) {
    echo "Welcome back, " . $_COOKIE['user'];
} else {
    echo "No cookie found!";
}

Now the user stays remembered for a week!

Step 3: Delete a Cookie

setcookie("user", "", time() - 3600, "/");

Poof! The cookie is gone!

Real-World Example: Remembering User Login

Let’s combine sessions & cookies for a simple "Remember Me" login system!

Login Form (index.php)

<form method="POST" action="login.php">
    <input type="text" name="username" placeholder="Enter your username">
    <input type="password" name="password" placeholder="Enter your password">
    <input type="checkbox" name="remember"> Remember Me
    <button type="submit">Login</button>
</form>

Processing Login (login.php)

session_start();
if ($_POST['username'] == 'John' && $_POST['password'] == '12345') {
    $_SESSION['user'] = $_POST['username'];
    
    if(isset($_POST['remember'])) {
        setcookie("user", $_POST['username'], time() + (86400 * 30), "/");
    }
    echo "Login successful!";
} else {
    echo "Invalid credentials!";
}

Now, the user stays logged in even after refreshing!

When to Use What?

  • Use Sessions when storing sensitive or temporary data (e.g., user authentication).
  • Use Cookies for long-term storage (e.g., remembering preferences, tracking visits).

Now, you’ve mastered PHP Sessions & CookiesSessions = Temporary memory (server-side), Cookies = Sticky notes (browser-side) 

Post a Comment

0 Comments