php - setcookie()已经设置了路径,为何不起作用?
黄舟
黄舟 2017-04-10 16:05:32
[PHP讨论组]
setcookie($name, $value , $_t, '/', $domain);

代码如上,按道理应该cookie是保存在跟站根目录,但是为什么访问 /aaa/bbb.html 这个cookie就保存到了/aaa目录下呢?我想要全站所有路径都保存到根目录下,怎么改代码哇?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
大家讲道理

PHP setcookie里的path参数并不是说是让你指定把cookie保存到哪里,而是说在访问指定的path时客户端才会把Cookie信息发送给服务端。默认值'/'是代表访问站点的每个path(也就是url)都会把cookie数据发送给服务端, 他对应的就是HTTP请求头部Set-Cookie里的path字段

cookie数据不会保存到你网站的根目录里,不管你把path和domain指定成什么,他都是保存在你电脑上的浏览器指定的一个目录里。 setcookie是服务端设置Cookie记录信息到客户端,客户端在访问服务器时根据设定的path、domain、secure等参数再把客户端的Cookie发送给服务器用。

PHPz

试试../

PHPz

http://php.net/manual/en/function.setcookie.php
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.

题主确认各种浏览器试过?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号