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 in give_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中?
发布评论