0
0
PHPprogramming~10 mins

How cookies work in PHP - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How cookies work
Client sends HTTP request
Server checks for cookies
Server sends HTTP response with Set-Cookie header
Browser stores cookie
Browser sends cookie with next requests
Server reads cookie from request
Repeat cycle
This flow shows how a browser and server exchange cookies through HTTP requests and responses.
Execution Sample
PHP
<?php
// Set a cookie
setcookie('user', 'Alice', time() + 3600);

// Read cookie
if (isset($_COOKIE['user'])) {
    echo 'Hello ' . $_COOKIE['user'];
}
?>
This PHP code sets a cookie named 'user' and then reads it to greet the user.
Execution Table
StepActionServer BehaviorBrowser BehaviorOutput
1Client sends first HTTP requestNo cookie receivedNo cookie stored yetNo output
2Server runs setcookie('user', 'Alice', time()+3600)Sends Set-Cookie header: user=Alice; expires=+1 hourReceives response with Set-Cookie headerNo output yet
3Browser stores cookie 'user=Alice'No actionStores cookie for domainNo output
4Client sends second HTTP requestReceives cookie 'user=Alice' in request headersSends cookie with requestNo output
5Server checks $_COOKIE['user']Finds 'user' cookie with value 'Alice'No actionOutputs: Hello Alice
6End of scriptResponse sent with greetingDisplays greetingHello Alice
💡 Execution stops after server sends greeting and browser displays it.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
$_COOKIE['user']undefinedundefinedundefined'Alice''Alice''Alice'
Cookie stored in browsernonenone'user=Alice''user=Alice''user=Alice''user=Alice'
Key Moments - 2 Insights
Why doesn't the cookie appear in $_COOKIE immediately after setcookie()?
Because setcookie() only sends a header to the browser; the cookie is stored on the client and sent back on the next request, as shown between steps 2 and 4 in the execution_table.
How does the browser know to send the cookie back to the server?
The browser stores the cookie after receiving the Set-Cookie header (step 3) and automatically includes it in the headers of subsequent requests (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the server first receive the cookie from the browser?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'Server Behavior' column to see when the cookie is received in the request headers.
According to variable_tracker, what is the value of $_COOKIE['user'] after the first request?
AAlice
Bnull
Cundefined
Dempty string
💡 Hint
Look at the 'After Step 2' column for $_COOKIE['user'] in variable_tracker.
If the cookie expiration time is set to time() - 3600, what would happen in the execution flow?
ABrowser deletes the cookie immediately
BBrowser stores cookie normally
CServer ignores the cookie
DCookie value changes to empty string
💡 Hint
Recall that setting expiration in the past causes the browser to delete the cookie.
Concept Snapshot
PHP cookies are small data stored on the browser.
Use setcookie() to send cookies from server to browser.
Browser stores cookies and sends them back with requests.
$_COOKIE array holds cookies sent by browser.
Cookies appear in $_COOKIE only on next request after setcookie().
Full Transcript
This visual trace shows how cookies work in PHP. First, the client sends a request without cookies. The server uses setcookie() to send a cookie in the response header. The browser stores this cookie and sends it back with the next request. The server reads the cookie from $_COOKIE and can use it, for example, to greet the user. Cookies are stored on the client and only appear in $_COOKIE on subsequent requests after being set. This cycle repeats with each request and response.