Cookies are small key-value pairs stored in the user's browser. PHP sends them with setcookie() and reads them via $_COOKIE.
setcookie("username", "Alice", time() + 3600); // 1 hour
setcookie("theme", "dark", time() + 86400 * 30); // 30 days
if (isset($_COOKIE["username"])) {
echo "Welcome back, " . htmlspecialchars($_COOKIE["username"]);
}
setcookie("username", "", time() - 3600); // expiry in the past
setcookie(
name: "session_token",
value: $token,
expires: time() + 3600,
path: "/",
domain: "",
secure: true, // HTTPS only
httponly: true // not accessible via JavaScript
);
setcookie("csrf_token", $token, [
"expires" => time() + 3600,
"path" => "/",
"secure" => true,
"httponly" => true,
"samesite" => "Strict" // or "Lax"
]);