首页 >后端开发 >PHP问题 > 正文

php设置cookie失败怎么办

原创2019-09-28 15:57:0801395

在某个页面中使用setcookie来设置cookie,例如:

setcookie("id",$id, time()+36002430);

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

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

相关推荐:《php入门教程

setcookie的第4个参数是path:

The 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设置cookie失败怎么办的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:php 设置 cookie 失败
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    推荐视频教程
  • PHP零基础视频教程PHP零基础视频教程
  • PHP每日小知识(第二季)PHP每日小知识(第二季)
  • PHP语法基础与数据库详解教程PHP语法基础与数据库详解教程
  • PHP数据库编程零基础入门到精通PHP数据库编程零基础入门到精通
  • 视频教程分类