How to access cookie values on different paths of the same domain using PHP?
Can a parameter of path be added 开发者_开发知识库to $_COOKIE[]?
In case you are trying to access cookies set for a different path on the same domain than the current, that can't be done. The browser itself restricts this, and only sends the cookies appropriate for the current path.
While the HTTP client (ie: browser) does not send back the path that the cookie was set to, PHP actually makes assumptions about cookies with regards to its $_COOKIE array.
If you set two cookies with the same name, one with the value "first-value" with path / and the second with the value "second-value" with path /test, a browser following the recommended - but not required - behaviour from the RFC will send back both values to you. When you access a URL under the /test path, the browser sends this:
Cookie: name=second-value, name=first-value
The "problem" is that PHP only reads the first value - $_COOKIE['name'] will only contain the value "second-value" with no hint that "first-value" exists. If you need access to both values, you need to parse the value of $_SERVER['HTTP_COOKIE'] yourself - this will contain "name=second-value, name=first-value" for the above example. Note that "second-value" is first in line because it was set with a longer path. Please note that the RFC does not guarantee this behaviour, it only says that HTTP clients SHOULD do this.
yes, it's the 4th argument, but you will only be able to access the cookie if it was set using a path that the current directory resides in.
that's confusing... here it is from php:
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.
http://php.net/manual/en/function.setcookie.php
You access it like any other cookie. It will become available in $_COOKIE if the script has access to it.
No, such parameters are impossible because the browser does not send the path to the server. It does only send the name and the value of each cookie (so you can't see the path, if it's a session cookie, when it will expires and so on).
I don't think it will be possible to get the cookie from a different path, since it could possibly cause a security issue.
精彩评论