PHP Cookie Handling Essentials Quiz
A 35-question quiz that examines how PHP applications set, scope, secure, and debug cookies for personalization, analytics, and authentication touchpoints.
Question 1
During a login callback, why do reviewers insist on calling setcookie before echoing any template output or UTF-8 BOM?
Question 2
A teammate captured this snippet after the controller finished processing. What does it echo?
<?php
$result = setcookie('theme', 'dark', time() + 3600, '/');
echo $result ? 'queued' : 'failed';
?>Question 3
Analytics wants to stuff tracking metadata into a single cookie. Why do architects cap each cookie at roughly 4 KB?
Question 4
Given this request-scoped snippet, what is rendered?
<?php
$_COOKIE['locale'] = 'fr-CA';
echo $_COOKIE['locale'] ?? 'fallback';
?>Question 5
Why do browser vendors require the Secure flag whenever SameSite=None is used for cross-site flows?
Question 6
Look at this diagnostic snippet executed during a request that already contains a feature cookie. What prints?
<?php
$_COOKIE['feature'] = 'beta';
setcookie('feature', 'stable', time() + 3600);
echo $_COOKIE['feature'];
?>Question 7
Product wants to keep structured preferences in one cookie. Why is storing a JSON string safer than serializing PHP objects directly?
Question 8
An engineer tests immediate availability of a freshly set cookie. What does this snippet echo?
<?php
setcookie('promo', 'BOGO', ['expires' => time() + 600, 'path' => '/deals']);
echo isset($_COOKIE['promo']) ? 'present' : 'not yet';
?>Question 9
Why does setting the cookie domain to .acme.fm matter when multiple subdomains share authentication?
Question 10
Security requested that every sensitive cookie be marked HttpOnly. What problem does that flag reduce?
Question 11
A controller guards against template fragments that might have already flushed output. What does this diagnostic snippet echo when no output was sent yet?
<?php
if (headers_sent()) {
echo 'fallback';
} else {
setcookie('ab', 'A');
echo 'queued';
}
?>Question 12
Why do platform teams standardize cookie paths (for example, /app versus /) even when the app only uses one route today?
Question 13
Why should sensitive cookie values (session IDs, CSRF secrets) never be logged verbatim even during debugging?
Question 14
How does this snippet interpret a structured cookie string?
<?php
$_COOKIE['prefs'] = 'lang=en&tz=utc';
parse_str($_COOKIE['prefs'], $prefs);
echo $prefs['lang'] ?? 'none';
?>Question 15
Why do teams avoid setting Secure=false on production cookies even for anonymous tracking?
Question 16
During privacy reviews, why do architects forbid copying entire user profiles into cookies for offline access?
Question 17
A migration splits a large value into multiple cookie chunks for legacy browsers. What does this snippet print?
<?php
$payload = str_repeat('x', 3000);
$chunks = str_split($payload, 1000);
foreach ($chunks as $index => $chunk) {
setcookie('report_' . $index, $chunk, 0, '/');
}
echo count($chunks);
?>Question 18
Why do logout flows call setcookie with an expiration in the past as well as clearing server-side state?
Question 19
Compliance asks for input validation on cookies before use. Which PHP API is most helpful for pulling a sanitized value?
Question 20
How many seats does this snippet report when the cookie arrives as a numeric string?
<?php
$_COOKIE['seats'] = '12';
echo (int) $_COOKIE['seats'] + 3;
?>Question 21
Why might a shared domain leave the cookie domain attribute blank even when subdomains exist?
Question 22
Frameworks often wrap setcookie inside response objects. Why do they still expose low-level cookie option maps?
Question 23
What does this snippet output after decoding a JSON preference cookie?
<?php
$_COOKIE['ui'] = '{"density":"compact","color":"midnight"}';
$prefs = json_decode($_COOKIE['ui'], true);
echo $prefs['color'] ?? 'default';
?>Question 24
Why do internationalized sites store only a locale code in cookies instead of the entire translation catalog?
Question 25
Support asks whether to use expires or max-age when setting cookies. Why do many teams prefer max-age when supported?
Question 26
How does this snippet validate numeric cookies before use?
<?php
$_COOKIE['cart_qty'] = '7';
$qty = filter_var($_COOKIE['cart_qty'], FILTER_VALIDATE_INT);
echo $qty === false ? 'invalid' : $qty;
?>Question 27
Why should CSRF tokens live in HttpOnly, same-site cookies instead of JavaScript-accessible storage?
Question 28
A product surface posts forms back to the same domain. Why is SameSite=Lax usually acceptable for its session cookie?
Question 29
Why do security guides recommend hashing remember-me tokens before storing them in cookies?
Question 30
A middleware signs cookie values to detect tampering. What does this snippet echo?
<?php
$secret = 'build-2025';
$_COOKIE['state'] = 'page=pricing';
$signature = hash_hmac('sha256', $_COOKIE['state'], $secret);
echo strlen($signature);
?>Question 31
Why do logout pages often clear both authentication and auxiliary personalization cookies?
Question 32
When would setrawcookie be preferred over setcookie for the same value?
Question 33
Why do multi-region platforms document cookie behavior per environment (staging, EU, US) even when code is shared?
Question 34
Why should teams monitor response headers after CDN or WAF changes?
Question 35
Why do incident responders keep a playbook for clearing or rotating cookies after breaches?
