Cookie trouble

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
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Cookie trouble

Post by oQEDo »

Hi guys,
I'm having a bit of trouble with cookies. I created a cookie with the following line:

Code: Select all

<?php
        setcookie("Projects", date("d/m/y H:i"),time()+8640000,"/");
?>
It creates a cookie called Projects which expires in 100 days containing a timestamp.

I want to update the cookie after certain transactions so I just use the same line but it's not updating my cookie, however if I refresh the page it does update my cookie!

Can any of you tell me if:
a) My code is correct or
b) Why it's only updating the cookie after a 2nd go (after a browser refresh)

Appreciated

Ron
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

the cookie is updated, but you can only see the update after you have refreshed the page.

you could put the cookie updateing code in another page and after updateding it redirect the user back :D.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

you use the page again if you set this cookie in this page?
that's wrong.
you can use it so:

1. Check the cookie if it's already set or not.
2.a) If not: set this cookie.
2.b) If it's set. Don't take any action about this cookie and load the page.

Have a nice day.
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Further information

Post by oQEDo »

qads,
I do set the cookie on another page. I have an engine page called _admin.php. However....

Just under the line where I set the cookie I have a redirect...

Code: Select all

<?php
        setcookie("Projects", date("d/m/y H:i"),time()+8640000,"/");
        header("Location:default.php");
?>
When I remove the redirect the cookie gets updated, if I leave the redirect in then the cookie does not get updated!

Is there something strange going on in the headers?

Any suggestions would be appreciated.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

hmm...

is _admin.php in another dir? e.g. dir/_admin.php?

cos that might be it, i never use the end slash when setting cookies..so this is worth a try:

Code: Select all

<?php
setcookie("Projects", date("d/m/y H:i"),time()+8640000); 
header("Location:default.php");


?>
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Post by oQEDo »

Thanks for your suggestion qads however it didn't make a difference.

I have also tried this:

Code: Select all

<?php
setcookie("Projects", date("d/m/y H:i"),time()+8640000); 
print "<a href=default.php>Back</a>"; 
?>
This works fine, the cookie is updated! It just doesn't work with a redirect!

Bizarre :x
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

No idea to solve your problem, but interesting subject (sorry, cant test either, hardware fubared).
Does the header() break it if you add a sleep() or usleep() before it?

I have a wierd thought that the header is to fast for the setcookie. Unlikely, but...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you on IIS? The setcookie() then header() thing doesn't work correctly on that IIRC - always had to fix a couple of lines in phpMyAdmin to account for the bug.

Mac
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Post by oQEDo »

twigletmac,
Yes, indeed I am running on IIS - can you suggest which lines I should be looking at in phpMyAdmin?
Post Reply