首页 > setcookie()已经设置了路径,为何不起作用?

setcookie()已经设置了路径,为何不起作用?

setcookie($name, $value , $_t, '/', $domain);

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


试试../


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

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


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.

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

【热门文章】
【热门文章】