Guides
› Intermediate
Intermediate PHP: Sessions & Cookies
Go beyond the basics and master stateful web interactions in PHP using sessions and cookies — key tools for building dynamic, user-aware applications.
Contents · 9 sections
Cookies vs. Sessions
Creating Cookies
Retrieving & Updating
Deleting Cookies
Understanding Sessions
Storing Session Data
Accessing & Unsetting
Destroying Sessions
Cookie Support Check
Cookies vs. Sessions
Cookies are stored on the client and automatically sent with requests. Sessions are stored on the server and offer more secure, temporary state management.
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
session_start();
$_SESSION['username'] = 'Natalie';
echo "Welcome, " . $_SESSION['username'] . "!";
?>
Creating Cookies
Use setcookie() to create cookies. Only the name is required; other parameters are optional.
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
setcookie("user_token", "abc123xyz", time() + 3600, "/");
?>
Retrieving & Updating
Access cookies via $_COOKIE. Updating is done by calling setcookie() again with the same name and new values.
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
echo $_COOKIE["user_token"];
setcookie("user_token", "updatedValue", time() + 7200, "/");
?>
Deleting Cookies
Expire a cookie by setting its time in the past:
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
setcookie("user_token", "", time() - 3600, "/");
?>
Understanding Sessions
Sessions are server-side containers for user data. Start them with session_start() before any output.
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
session_start();
$_SESSION['username'] = 'Natalie';
$_SESSION['logged_in'] = true;
if ($_SESSION['logged_in']) {
echo "Hello, " . $_SESSION['username'] . "! You are logged in.";
}
?>
Storing Session Data
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
session_start();
$_SESSION["username"] = "Natalie";
?>
Accessing & Unsetting
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
session_start();
echo $_SESSION["username"];
unset($_SESSION["username"]);
?>
Destroying Sessions
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
session_start();
session_destroy();
?>
Cookie Support Check
php
{ copied = true; setTimeout(() => copied = false, 2000) })"
class="rounded border border-gray-600 px-2.5 py-1 text-gray-200 transition-colors hover:border-gray-400 hover:text-white">
Copy
Copied!
<?php
if (count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
Understanding cookies and sessions is crucial for secure, personalized PHP applications. Explore advanced usage in login systems and user tracking next.
This might also interest you
PHP 5.6
optimization
forum thread
mod_rewrite
user-friendly URLs
PHP
PHP
VB.NET
variable passing