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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

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

Post 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);

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

?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Excellent, I wasn't aware of that, so I guess we all benefit?
murph
Forum Commoner
Posts: 29
Joined: Fri Oct 03, 2003 1:28 pm
Location: washington

Post 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 -
Post Reply