Handling Sessions and Cookies
Manage user sessions and store data with cookies.
Handling Sessions and Cookies is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Handling Sessions and Cookies
Welcome to the lesson on handling sessions and cookies! In this lesson, you’ll learn how to manage user data across multiple pages using PHP’s session and cookie features. Let’s dive in!

2
What Are Sessions and Cookies?
Sessions: A session stores user data on the server and associates it with a unique session ID. It is used for managing data during a user’s visit to a website.
Cookies: A cookie is a small piece of data stored on the user's browser. It is used to persist data between visits.
Key Difference: Sessions store data on the server, while cookies store data on the client-side.
3
Starting a Session
To use sessions in PHP, you need to start the session using the session_start() function. Example:
Tip: Always call session_start() at the beginning of your script before any output.
<?php
session_start(); // Start the session
// Store data in the session
$_SESSION["username"] = "JohnDoe";
echo "Session started. Username: " . $_SESSION["username"];
?>
4
Accessing Session Data
You can access session data using the $_SESSION superglobal. Example:
Key Point: Use isset() to check if session data exists.
<?php
session_start();
if (isset($_SESSION["username"])) {
echo "Welcome back, " . $_SESSION["username"];
} else {
echo "No session data found.";
}
?>
5
Destroying a Session
To log out a user or clear session data, use the session_destroy() function. Example:
Tip: Use unset() to remove specific session variables without destroying the entire session.
<?php
session_start();
// Destroy the session
session_destroy();
echo "Session destroyed. You are logged out.";
?>
6
Setting a Cookie
Use the setcookie() function to store a cookie on the user’s browser. Example:
Key Parameters:
name: The name of the cookie.value: The value of the cookie.expire: Expiration time in seconds.path: The directory where the cookie is accessible.
<?php
setcookie("user", "JohnDoe", time() + (86400 * 7), "/"); // Set a cookie for 7 days
echo "Cookie set. User: JohnDoe";
?>
7
Accessing a Cookie
Access cookies using the $_COOKIE superglobal. Example:
Tip: Use isset() to check if a cookie exists before accessing it.
<?php
if (isset($_COOKIE["user"])) {
echo "Welcome back, " . $_COOKIE["user"];
} else {
echo "No cookie found.";
}
?>
8
Deleting a Cookie
To delete a cookie, set its expiration time to a past date. Example:
Tip: Ensure the path matches the one used when setting the cookie.
<?php
setcookie("user", "", time() - 3600, "/"); // Delete the cookie
echo "Cookie deleted.";
?>
9
10
Great Job!
Congratulations! You’ve learned how to handle sessions and cookies in PHP. These tools are essential for managing user data and creating personalized experiences. In the next lesson, we’ll explore advanced session management techniques, including securing and regenerating session IDs. Let’s keep coding!

Frequently asked questions
Is the “Handling Sessions and Cookies” lesson free?
Yes — the full text of “Handling Sessions and Cookies” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Handling Sessions and Cookies”?
Manage user sessions and store data with cookies. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Handling Sessions and Cookies” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Handling Sessions and Cookies
- Working with Files
- Error and Exception Handling
- Using Regular Expressions