Cookie不会持久存储在PHP中?

编程入门 行业动态 更新时间:2024-10-11 17:23:08
本文介绍了Cookie不会持久存储在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 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中?

本文发布于:2023-05-29 03:39:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!