Page 1 of 1

Cookie trouble

Posted: Fri Sep 05, 2003 5:48 am
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

Posted: Fri Sep 05, 2003 6:56 am
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.

Posted: Fri Sep 05, 2003 7:20 am
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.

Further information

Posted: Fri Sep 05, 2003 9:37 am
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.

Posted: Fri Sep 05, 2003 10:52 am
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");


?>

Posted: Mon Sep 08, 2003 6:15 am
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

Posted: Mon Sep 08, 2003 8:36 am
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...

Posted: Mon Sep 08, 2003 9:27 am
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

Posted: Mon Sep 08, 2003 10:23 am
by oQEDo
twigletmac,
Yes, indeed I am running on IIS - can you suggest which lines I should be looking at in phpMyAdmin?