What is the correct way to set and retrieve a cookie in PHP?
To set a cookie in PHP, you can use the `setcookie()` function. This function takes parameters for the cookie name, value, expiration time, path, domain, and secure flag. To retrieve a cookie, you can use the `$_COOKIE` superglobal array. Make sure to set the cookie before any output is sent to the browser.
// Setting a cookie
setcookie("user", "John Doe", time() + 3600, "/");
// Retrieving a cookie
if(isset($_COOKIE['user'])) {
$user = $_COOKIE['user'];
echo "Hello, $user!";
} else {
echo "Cookie not set!";
}