Write a PHP script that sets a cookie with user preferences and retrieves it on subsequent visits.

<?php
$cookie_name = "user_preferences";
$cookie_value = "dark_mode";

// Set cookie to expire in 30 days
setcookie($cookie_name, $cookie_value, time() + (30 * 24 * 60 * 60), "/");

// Retrieve and display cookie value
if (isset($_COOKIE[$cookie_name])) {
    echo "User Preference: " . $_COOKIE[$cookie_name];
} else {
    echo "Cookie not set";
}
?>

 

Post your Answer