我是一个PHP初学者,在刷新页面后,我似乎找不到关于会话或cookie的问题的答案。
我想要的是在PHP中存储一个布尔值-如果它是页面的第一次加载为true,如果不是为false(例如,它是刷新后的加载或类似的东西)。
你能帮我一下吗?谢谢你。$forAnimation;
if(isset($_COOKIE["pollChart"])){
$forAnimation = false;
} else {
setcookie("pollChart");
$forAnimation = true;
}
cookie非常简单,您只需设置密钥、值、过期时间等。您可以在手册(http://php.net/manual/en/function.setcookie.php):
)中查看它的工作原理。// If the cookie is not set
if(!isset($_COOKIE["pollChart"])){
// Set cookie key and string value
setcookie("pollChart",json_encode(array('set'=>false)));
// If set already, change value
} else {
// Decode array
$dec = json_decode($_COOKIE['pollChart'],true);
// If the set value is true
if($dec['set'])
// Expire the cookie
setcookie("pollChart",'',(time()-3600));
// If set to false turn it to true
else
setcookie("pollChart",json_encode(array('set'=>true)));
}
// Get the contents of the cookies set
print_r($_COOKIE);
给你在第一次加载:
Array
(
[PHPSESSID] => 4c4871ac239cec6fa2f3f43bcbed6d81
[pollChart] => {"set":false}
)
第二个载荷:
Array
(
[PHPSESSID] => 4c4871ac239cec6fa2f3f43bcbed6d81
[pollChart] => {"set":true}
)
最后加载:
Array
(
[PHPSESSID] => 4c4871ac239cec6fa2f3f43bcbed6d81
)