Cookie不会持久存储在PHP中? [英] Cookies aren't persisting in PHP?
问题描述
如何让Cookie在php中保留?
give_cookie.php
<?php
if(!isset($ _ COOKIE [muffin]))
setcookie(松饼,55,100 * 60 * 60 * 24 * 30);
$ _COOKIE [lid] = true;
?>
jar.php
$ b b
<?php
var_dump($ _ COOKIE);
if($ _ COOKIE [lid])
echoopen;
?>
按顺序运行代码,
array(0){}注意:未定义的索引:jar在第3行的jar.php中
将
中的代码嵌入
给我输出:
中的jar.php
array(1){[lid] => bool b $ b
解决方案你应该给出一个UNIX时间戳记cookie何时过期函数调用的第三个参数。
Cookie到期的时间。这是一个Unix时间戳,所以从时代起的秒数为
。换句话说,你很可能将
设置为time()函数加上
希望它过期的秒数。或者你可以使用mktime()。 time()+ 60 * 60 * 24 * 30将
设置为cookie在30天后过期。如果设置为0或省略,
cookie将在会话结束时过期(当浏览器
关闭时)。
我建议您阅读
setcookie
。How do I get the cookies to persist in php?
give_cookie.php
<?php if (!isset($_COOKIE["muffin"])) setcookie("muffin", "55", 100 * 60 * 60 * 24 * 30); $_COOKIE["lid"]=true; ?>
jar.php
<?php var_dump($_COOKIE); if($_COOKIE["lid"]) echo "open"; ?>
Running the code in that order gives me output:
array(0) { } Notice: Undefined index: lid in jar.php on line 3
Embedding the code from
jar.php
ingive_cookie.php
gives me output:array(1) { ["lid"]=> bool(true) } open
解决方案You're supposed to give a UNIX timestamp of when the cookie will expired (calculated since the epoch) as the third argument to the function call.
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
I suggest you read the documentation for
setcookie
.这篇关于Cookie不会持久存储在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!