tags:

views:

606

answers:

3

My application needs to set cookies for specific paths in the application. For example (in php):

setcookie(*cookie_name*,*value*,*date*,"/subpath/subpath/unique_name");
setcookie(*cookie_name*,*value*,*date*,"/subpath/subpath/another unique name");

Oddly enough, the first setcookie works fine. The second doesn't generate an error and when I view my cookies in Firefox the cookie is there with the correct values. However, I can't access it in my code. I believe the whitespaces are causing the trouble but I haven't found any documentation or specs on how cookie paths should be encoded.

Has anyone encountered this problem before? Does anyone know how to deal with special characters in cookie paths?

A: 

No access to a webserver atm. You haven't tried one of these?

setcookie(*cookie_name*,*value*,*date*, "/subpath/subpath/another unique name/");
setcookie(*cookie_name*,*value*,*date*, urlencode("/subpath/subpath/another unique name"));
setcookie(*cookie_name*,*value*,*date*, rawurlencode("/subpath/subpath/another unique name"));

I believe different browsers and web servers might treat these differently. I hope you don't have to use whitespace in the url.

OIS
A: 

What OS and webserver are you running on?

You can try %20 or + encoding spaces, as suggested.

Also, is the path you're trying a real directory, or a rewrite-rule?

singpolyma
+1  A: 

I see no problem with spaces in cookies.
Maybe you should check how you read back your value...
My read routine is:

function ReadCookie(name)
{
  name += '=';
  var parts = document.cookie.split(/;\s*/);
  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (part.indexOf(name) == 0)
      return part.substring(name.length)
  }
  return null;
}

Obviously, the only thing you have to encode is semi-colon.

PhiLho