温馨提示:本文翻译自stackoverflow.com,查看原文请点击:cookies - How to easily hide an echo after submit in a PHP form?

cookies - 如何以PHP表单提交后轻松隐藏回显?

发布于 2020-03-29 22:05:39

我正在使用cookie,我需要显示一条消息,说直到设置表单中的值后才设置'x'cookie,然后单击提交后,我需要显示一条消息,说已设置'x'cookie ,但我需要“隐藏”上一条消息。

<?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];
 } 
}
?>

干杯。

查看更多

提问者
Rex Applegate
被浏览
42
CodyKL 2020-01-31 19:02

那是基础!

$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;