作者:zhoujielcl_767 | 来源:互联网 | 2020-09-09 18:42
phpcookie失效是因为当setcookie中第4个参数为空的话,默认只在当前目录生效,其解决办法就是添加第4个参数,语句为“setcookie("id",$id,time()+36002430,);”。
php COOKIE失效是因为当setCOOKIE中第4个参数为空的话,默认只在当前目录生效,其解决办法就是添加第4个参数,语句为“setCOOKIE("id",$id, time()+36002430 ,'/');”。
PHP设置COOKIE无效问题原因以及解决方案
在某个页面中使用setCOOKIE来设置COOKIE,例如
setCOOKIE("id",$id, time()+36002430);
但是回到首页之后发现没有生效,用Javascript:alert(document.COOKIE)里面为空,PHP里面的$_COOKIE也是没有数据。
跑到PHP官网查看setCOOKIE的说明,官网的例子也是这样的,但是仔细看来参数说明之后就发现问题了。
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中文网!
以上就是php COOKIE失效怎么办的详细内容,更多请关注 第一PHP社区 其它相关文章!