php cookie失效,php cookie失效怎么办

php cookie失效是因为当setcookie中第4个参数为空的话,默认只在当前目录生效,其解决办法就是添加第4个参数,语句为“setcookie("id",$id, time()+36002430 ,'/');”。

b68f0ae8745ebe0f6daf69bceb58b1b4.png

PHP设置cookie无效问题原因以及解决方案

在某个页面中使用setcookie来设置cookie,例如setcookie("id",$id, time()+36002430);

但是回到首页之后发现没有生效,用javascript:alert(document.cookie)里面为空,PHP里面的$_COOKIE也是没有数据。

跑到PHP官网查看setcookie的说明,官网的例子也是这样的,但是仔细看来参数说明之后就发现问题了。

setcookie的第4个参数是pathThe path on the server in which the cookie will be available on. If set to ‘/’, the cookie will be available within the entire domain. If set to ‘/foo/’, the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

也就是说如果第4个参数为空的话,默认只在当前目录生效,一般情况下是没有问题的。

但是我的站点设置了rewrite,把index.php给隐去了,所以设置的cookie变成只在该页面有效。

解决方案就是添加第4个参数setcookie("id",$id, time()+36002430 ,'/');

更多相关知识,请访问PHP中文网!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值