Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php // Set a cookie (must be called before any output) setcookie('theme', 'dark', [ 'expires' => time() + (86400 * 30), // 30 days 'path' => '/', 'domain' => 'example.com', 'secure' => true, // HTTPS only 'httponly' => true, // not accessible via JS 'samesite' => 'Lax', ]); // Read a cookie on subsequent requests $theme = $_COOKIE['theme'] ?? 'light'; echo "Current theme: $theme"; // Update a cookie — just call setcookie() again with the same name setcookie('theme', 'light', [ 'expires' => time() + (86400 * 30), 'path' => '/', 'httponly' => true, 'samesite' => 'Lax', ]); // Delete a cookie — set expiry in the past setcookie('theme', '', [ 'expires' => time() - 3600, 'path' => '/', ]); // Check if a cookie exists if (isset($_COOKIE['theme'])) { echo "Theme preference found: " . htmlspecialchars($_COOKIE['theme']); }
Result
Open