views:

142

answers:

4

Quick question: One can set the path where a cookie is valid, but is it also possible to get (read) this path from the cookie (in PHP)?

Or else: is it possible to extend a cookie's time, without knowing what path it's on (but keeping the path the same)?

A: 

There is no way for PHP to read path of the cookie because browser sends to the server only values of the cookies that should be sent, nothing more.

You might try to re-set cookie without giving path but I highly doubt it will work. There might be many different relevant cookies with same name and less and more precise paths. If you tried to set up cookie with same name but without path browser would not know which of the cookies it should update (maybe the one wit most precise path? but that could lead to (security?) errors when precise cookie you expect to be set is not set).

Kamil Szot
+1  A: 

Since most browsers still use Netscape’s specification (see cached version of http://wp.netscape.com/newsref/std/cookie_spec.html) and not the one specified in RFC 2109 or RFC 2965, the list elements in the Cookie request header field will only consist of the name and value pair:

When requesting a URL from an HTTP server, the browser will match the URL against all cookies and if any of them match, a line containing the name/value pairs of all matching cookies will be included in the HTTP request. Here is the format of that line:

Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ...

Only the newer specifications (RFC 2109 and RFC 2965) allow the client to send the path within the request (excerpt from RFC 2109):

The syntax for the header is:

cookie          =       "Cookie:" cookie-version
                        1*((";" | ",") cookie-value)
cookie-value    =       NAME "=" VALUE [";" path] [";" domain]
cookie-version  =       "$Version" "=" value
NAME            =       attr
VALUE           =       value
path            =       "$Path" "=" value
domain          =       "$Domain" "=" value

[…] The value for the path attribute must be the value from the Path attribute, if any, of the corresponding Set-Cookie response header. Otherwise the attribute should be omitted from the Cookie request header. […]

Gumbo
Ah right, and I can't expect all users to have this newer specification. So I'll need to find another approach.
RemiX
@RemiX: No, unfortunately most browsers still use the the old specification.
Gumbo
A: 

Hi,

yes you can change/extend the cookie time when the cookie is set as root cookie. That is you should set the cookie with "/" in the last parameter. see the code below...

setcookie("Message", $msg, time()+60, "/");

if not, basically the cookies will be stored in the current path (page root). you can try to access it.

VAC-Prabhu
Thanks, but unfortunately I also want it to work when it's not a root cookie (if I only used root cookies I would know the 'path' already and I wouldn't have this problem).
RemiX
A: 

include the path into cookie value too

StoneHeart