Php 饼干仅设置为 1 秒


Php cookie only set for 1 second?

也许有更好的方法来使用cookie。但是我想在我的 php 中设置一些东西来告诉用户一个错误,但只让它出现一次,如果他们刷新页面,我不希望它在那里了。

$time = (1000);
setcookie("msgtype", $type, time() + ($time), '/');
setcookie("msg", $message, time() + ($time), '/');
print_r($_COOKIE);

所以出于某种原因,如果我在它之后直接输出 $_cookie,它仍然不会显示这个 cookie。

有什么想法吗?

相反,当显示错误时,清除cookie,而不是将其设置为1秒,以便下次刷新并检查cookie时,它不会在那里,因此不会显示任何错误。

Or a better option would be using sessions.

您不需要为此使用 cookie,使用 SESSION 听起来很合理。

$_SESSION['error'] = 'soem message';
//Use it when you need and simply remove it afterwards
echo $_SESSION['error'];
unset($_SESSION['error']);

在你的例子中,我将使用会话

集合示例:

session_start()
$_SESSION['error'] = 1;

何时显示错误,请使用此:)

unset($_SESSION['error'];

$_SESSION['error'] = 0;

这将取决于使用情况:)