Page 1 of 2

cookies for a different page

Posted: Thu Sep 29, 2005 4:56 pm
by birdie
hi ive made a script which sets a cookie on the main page. This main page has links to a different page which also requires to read the cookie that i set. Although, this page which requires the cookie also has another cookie required but it does have a different cookie name. Why does the link also send the cookie for the previously set cookie? thanks :)

Posted: Thu Sep 29, 2005 5:21 pm
by Burrito
cookies are stored on the client which means they will stay on every page until their expiration time is met.

Posted: Thu Sep 29, 2005 5:22 pm
by pickle
Cookie's are stored client side, so they travel with the user, not the page. When you set the cookie, by default the cookie will work for all pages in the same domain (ie, set a cookie at http://www.foobar.com/testpage1.php and it will be available in all pages on http://www.foobar.com).

Of course, the client needs to have cookies turned on as well, in order for any cookies to work.

Posted: Thu Sep 29, 2005 5:26 pm
by birdie
yes... but my cookie is not present on the page and its all on the same domain.. it might be one of my scripts from ages ago that might have deleted all of the cookies except its own. what do u lot think?

Posted: Thu Sep 29, 2005 6:29 pm
by Burrito
it's hard to say w/o seeing your code...not to mention that I don't fully understand what your problem is 8O

Posted: Thu Sep 29, 2005 7:31 pm
by mickd
you also need to refresh/go to another page after you set the cookie.

Posted: Fri Sep 30, 2005 1:57 pm
by birdie
yes well the code is kind of huge so i'll give an example..

first page

Code: Select all

<? 
setcookie(birdiecookie,"hello");
?>
<a href="cookiepage.php">Click here</a>

cookiepage.php

Code: Select all

<?
$mycookie = $_COOKIE['birdiecookie'];
//another cookie from cookiepage.php is already in this page called birdiecookie2
//probably deletes all of the cookies if any code is present. the birdiecookie2 is from a website database like phpnuke.
echo $mycookie; //nothing happens. no birdiecookie present
?>
understand? thanks

Posted: Fri Sep 30, 2005 2:23 pm
by pickle
On cookiepage.php, just call print_r($_COOKIE); and see what you get

Posted: Fri Sep 30, 2005 4:31 pm
by birdie
hmm my cookie doesnt display in the array. is there any way to make the cookie not deletable by this page?

Posted: Fri Sep 30, 2005 4:34 pm
by pickle
Nope. Although if you do have code that deletes all cookies, I'd suggest making a special case for the cookies you DO want to keep.

Posted: Fri Sep 30, 2005 4:36 pm
by birdie
how would i do that, i have two cookies that i would like to keep. thanks

Posted: Fri Sep 30, 2005 4:37 pm
by pickle
Well you said you had a page that deleted all cookies when it was loaded. Go into that page and re-code it so it doesn't delete the two cookies you want to keep.

Posted: Fri Sep 30, 2005 4:40 pm
by birdie
hmm but this is an "e107 script", what would be the delete cookie function? this may interupt the script.. thanks for the help pickle

Posted: Fri Sep 30, 2005 4:42 pm
by Burrito
I'd be anxious to see how you're "deleting" the cookie.

the only way I know to "delete" a cookie is to reset its expiration to a time in the past.

Posted: Fri Sep 30, 2005 4:45 pm
by birdie
hmm so that means the script would need to know what the cookie was called... it doesnt.. weird. i'm going to try a different way just to make things easier. thanks for the help guys