cookies for a different page

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

birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

cookies for a different page

Post 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 :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

cookies are stored on the client which means they will stay on every page until their expiration time is met.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

you also need to refresh/go to another page after you set the cookie.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

On cookiepage.php, just call print_r($_COOKIE); and see what you get
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post by birdie »

hmm my cookie doesnt display in the array. is there any way to make the cookie not deletable by this page?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post by birdie »

how would i do that, i have two cookies that i would like to keep. thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
birdie
Forum Newbie
Posts: 22
Joined: Thu Sep 15, 2005 4:37 pm

Post 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
Last edited by birdie on Fri Sep 30, 2005 4:50 pm, edited 1 time in total.
Post Reply