简体   繁体   English

如何使“ Cookie”在某些路径中可用,但在其子目录中不可用

[英]How to make `cookie` available in certain path but not in its subdirectories

In php , I use setcookie to create a cookie, as the docs says, if you do not specify the path parameter, the cookie will be available in the current directory and all of its sub-directories . 文档所述,在php ,我使用setcookie来创建cookie,如果您未指定path参数,则cookie将在当前目录及其所有子目录中可用。 But I need the cookie will be available only in current directory and not in its sub-directories. 但是我需要cookie仅在当前目录中可用,而在其子目录中不可用。 How can I achieve this? 我该如何实现?

//it is available in current directory and all of its sub-directories:
setcookie("TestCookie", $value);
//with the same effect:
setcookie("TestCookie", $value,/current-directory/);

I want to exclude /current-directory/* 我想排除/current-directory/*

Thank you very much 非常感谢你

As far as I know, this is not possible. 据我所知,这是不可能的。

But you could store your information the $_SESSION variable and do the path check by yourself using $_SERVER["REQUEST_URI"] . 但是您可以将信息存储在$_SESSION变量中,并使用$_SERVER["REQUEST_URI"]自己进行路径检查。

The path option 路径选项

Another way to control when the Cookie header will be sent is to specify the path option. 控制Cookie头何时发送的另一种方法是指定path选项。 Similar to the domain option, path indicates a URL path that must exist in the requested resource before sending the Cookie header. 与域选项类似,path表示在发送Cookie头之前,请求的资源中必须存在的URL路径。 This comparison is done by comparing the option value character-by-character against the start of the request URL. 通过将选项值的每个字符与请求URL的开始进行比较来完成此比较。 If the characters match, then the Cookie header is sent. 如果字符匹配,则发送Cookie标头。 Sample: 样品:

Set-Cookie: name=Nicholas; path=/blog

In this example, the path option would match /blog, /blogroll, etc.; 在此示例中,路径选项将与/ blog,/ blogroll等匹配; anything that begins with /blog is valid. 以/ blog开头的任何内容均有效。 Note that this comparison is only done once the domain option has been verified. 请注意,只有在验证了域选项后才能进行此比较。 The default value for the path option is the path of the URL that sent the Set-Cookie header. path选项的默认值是发送Set-Cookie标头的URL的路径。

source: https://www.nczonline.net/blog/2009/05/05/http-cookies-explained/ 来源: https : //www.nczonline.net/blog/2009/05/05/http-cookies-explained/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM