php cookie失效怎么办_后端开发

php cookie失效怎么办_后端开发,第1张

如何修改php.ini无效问题_后端开发

修改“php.ini”无效的方法:首先用root账号,登录到nobody的账户;然后用“whereis php”命令查看“php.ini”所在的位置;接着修改“php.ini”文件;最后重启apache即可。


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


php cookie失效怎么办_后端开发,第2张

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

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

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

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


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


php在网页登录成功后怎么实现网页跳转_后端开发

php在网页登录成功后实现网页跳转的方法:首先打开编辑器,新建php文件;然后在php文件中输入代码“header('Location: index.php');”;最后在浏览器运行,此时会跳转到index.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 ,'/');

更多相关知识,请访问ki4网!

以上就是php cookie失效怎么办的详细内容,更多请关注ki4网其它相关文章!

Ubuntu彻底删除PHP7.0的方法_后端开发

Ubuntu彻底删除PHP的方法:首先通过命令“sudo apt-get autoremove php7*”删除php的相关包及配置;然后删除关联;接着清除dept列表;最后检查是否卸载干净即可。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/561875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2020-07-08
下一篇 2020-07-08

发表评论

登录后才能评论

评论列表(0条)

保存