Page 1 of 1

Cookies - can read from one directory and not another ??

Posted: Thu Oct 09, 2003 5:29 pm
by lazy_yogi
Hi,

After setting a cookie with
setcookie('NsODyRhEkx', '1', time()+60*60*24*300, "/" );

I've got this code below.
Then I run it in one directory it works and shows that that cookie was set.
But when I run it in the directory that contains this directory, it shows that cookie as not being set.

Any ideas why this is?
I'd like to keep the directory structure the same for the backend design of the site to remain structured.

Thanx for you help,
Eli

Code: Select all

<?

# --------------------------------------------------- #
if (isset($_COOKIE&#1111;'NsODyRhEkx'])) &#123;
  print "set::".$_COOKIE&#1111;'NsODyRhEkx'];
&#125; else  &#123;
  print "not set";
&#125;

# --------------------------------------------------- #

print "<hr>";
print_r($_COOKIE);

# --------------------------------------------------- #

?>

Posted: Thu Oct 09, 2003 9:11 pm
by nigma
As far as I know you cannot accesss a cookie from a different directory unless you specify that directory as the directory argument when setting the cookie.

Posted: Thu Oct 09, 2003 11:30 pm
by lazy_yogi
ahhh ... after going back to php.net reference under setcookie, I read this under path:
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.
apparently setting "/" as the path didn't work, I needed '/' for some reason.
Anyway, its working now.
Just thought I'd mention it incase anyone else needed to know.

Cheers

Posted: Fri Oct 10, 2003 12:41 am
by nigma
Excellent, I wasn't aware of that, so I guess we all benefit?

Posted: Fri Oct 10, 2003 2:40 pm
by murph
Also look into sessions, i think sessions are 10x better than cookies. yes they do the same thing but you dont have to worry about headers and such, and they expire when the user exits the browser. although that could be considered a + or -