在某个页面中运用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失利怎么办的细致内容,更多请关注ki4网别的相干文章!