Practical Web Data Patterns
Cookie Choices
Use a cookie-like value to choose a display preference.
cookie-choice
Cookies often store small preference values. A handler can validate the value and fall back to a default.
Cookie Choices
cookie_choice.php
Replay: real traced execution (multi-file project)
<?php
$theme = "dark";
$allowed = in_array($theme, ["light", "dark"], true);
$selected = $allowed ? $theme : "light";
echo "theme=" . $theme . "\n";
echo "selected=" . $selected . "\n";
<?php
$theme = "light";
$allowed = in_array($theme, ["light", "dark"], true);
$selected = $allowed ? $theme : "light";
echo "theme=" . $theme . "\n";
echo "selected=" . $selected . "\n";
<?php
$theme = "blue";
$allowed = in_array($theme, ["light", "dark"], true);
$selected = $allowed ? $theme : "light";
echo "theme=" . $theme . "\n";
echo "selected=" . $selected . "\n";
$theme ← dark, $allowed ← 1, $selected ← dark
1<?php2$theme→ dark = "dark"; //@theme="light", "blue"3$allowed→ 1 = in_array($themedark, ["light", "dark"], true);4$selected→ dark = $allowed1 ? $theme : "light";56echo "theme=" . $themedark . "\n";7echo "selected=" . $selecteddark . "\n";outputtheme=dark selected=dark
$theme ← light, $allowed ← 1, $selected ← light
1<?php2$theme→ light = "light";3$allowed→ 1 = in_array($themelight, ["light", "dark"], true);4$selected→ light = $allowed1 ? $theme : "light";56echo "theme=" . $themelight . "\n";7echo "selected=" . $selectedlight . "\n";outputtheme=light selected=light
$theme ← blue, $allowed ← (empty), $selected ← light
1<?php2$theme→ blue = "blue";3$allowed→ (empty) = in_array($themeblue, ["light", "dark"], true);4$selected→ light = $allowed(empty) ? $theme : "light";56echo "theme=" . $themeblue . "\n";7echo "selected=" . $selectedlight . "\n";outputtheme=blue selected=light