I'm working with cookies and I need to show a message saying that 'x' cookie is not set until I introduce a value in a form, then after I click submit I need to show a message saying that 'x' cookie is set, but I need to 'hide' the previous message.
<?php
$cookie_name = NULL;
$cookie_value = NULL;
$cookie_name = "user";
$cookie_value = "Name Surname";
setcookie($cookie_name, $cookie_value, time()+86400 );
echo "Cookie named '" . $cookie_name . "' is not set!";
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
}
?>
Cheers.
That are basics!
$cookie_name = NULL;
$cookie_value = NULL;
$cookie_name = "user";
$cookie_value = "Name Surname";
setcookie($cookie_name, $cookie_value, time()+86400 );
// Inital value of output
$output = "Cookie named {$cookie_name} is not set!";
// Check if method is POST and if cookie is set
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_COOKIE[$cookie_name])) {
// Overwrite output
$output = "Cookie {$cookie_name} is set!<br>Value is: {$_COOKIE[$cookie_name]}";
}
// Show output
echo $ouput;
Nevermind I had the output inside the if, now its working fine. Thank you.