0
0
PHPprogramming~10 mins

Setting and reading cookies in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting and reading cookies
Start PHP script
Set cookie with setcookie()
Send HTTP headers with cookie
Browser stores cookie
Next request: browser sends cookie
Read cookie from $_COOKIE superglobal
Use cookie value in script
End script
The script sets a cookie which the browser stores and sends back on next requests, allowing the script to read it from $_COOKIE.
Execution Sample
PHP
<?php
setcookie("user", "Alice", time() + 3600);

if (isset($_COOKIE["user"])) {
    echo "User: " . $_COOKIE["user"];
} else {
    echo "No user cookie set.";
}
?>
This code sets a cookie named 'user' with value 'Alice' for 1 hour, then reads and prints it if available.
Execution Table
StepActionEvaluationResult
1Call setcookie("user", "Alice", time()+3600)Sets HTTP header to set cookieCookie header sent to browser
2End script, browser receives cookie headerBrowser stores cookie 'user=Alice'Cookie saved in browser
3Next request sent by browserBrowser sends cookie 'user=Alice' in HTTP headersPHP $_COOKIE['user'] = 'Alice'
4Check if isset($_COOKIE['user'])TrueEnter if block
5Echo 'User: ' . $_COOKIE['user']Concatenate stringsOutput: User: Alice
💡 Script ends after output; cookie persists for 1 hour in browser.
Variable Tracker
VariableStartAfter Step 3After Step 5
$_COOKIE['user']undefinedAliceAlice
Key Moments - 2 Insights
Why does the cookie value appear only on the next request, not immediately after setcookie()?
Because setcookie() sends a header to the browser, which stores the cookie and sends it back on the next request. The $_COOKIE array is populated from incoming request headers, so it won't have the new cookie until the next page load (see execution_table step 3).
What happens if you try to read $_COOKIE['user'] before calling setcookie()?
If the cookie was not previously set and sent by the browser, $_COOKIE['user'] will be undefined or not set. The script must check with isset() to avoid errors (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the browser send the cookie back to PHP?
AStep 3
BStep 1
CStep 5
DStep 2
💡 Hint
Check the 'Action' and 'Result' columns at step 3 where the browser sends the cookie in HTTP headers.
According to the variable tracker, what is the value of $_COOKIE['user'] before the browser sends the cookie?
AAlice
Bnull
Cundefined
Dempty string
💡 Hint
Look at the 'Start' column for $_COOKIE['user'] in the variable tracker.
If the cookie expiration time is set to time() - 3600, what would happen?
ACookie is set and available immediately
BCookie is deleted or expired immediately
CCookie lasts for 1 hour
DCookie value becomes empty string
💡 Hint
Recall that setting expiration in the past deletes the cookie; see concept flow about cookie lifetime.
Concept Snapshot
setcookie(name, value, expire) sends a cookie header to browser.
Browser stores cookie and sends it back on next requests.
Read cookies in PHP from $_COOKIE superglobal.
$_COOKIE only has cookies sent by browser in current request.
Use isset() to check if cookie exists before reading.
Cookie expires after given time or when deleted.
Full Transcript
This visual execution shows how PHP sets and reads cookies. First, setcookie() sends a header to the browser to store a cookie named 'user' with value 'Alice' for one hour. The browser saves this cookie and sends it back in the next request. PHP reads the cookie from the $_COOKIE array, which is populated from the browser's request headers. The script checks if the cookie exists using isset() and then prints its value. Cookies do not appear in $_COOKIE immediately after setcookie() because the browser must send them back on the next request. Setting a cookie expiration time in the past deletes the cookie. This step-by-step trace helps beginners understand the flow of setting and reading cookies in PHP.