Cookie won't set...

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
Bitweiser
Forum Newbie
Posts: 11
Joined: Sun Jul 30, 2006 9:27 am

Cookie won't set...

Post by Bitweiser »

Hello Guy's,

I've got a strange thing going on.

I'm writing a cookie as follows:
setcookie("CookieName", $CookieValue, time()+3000);
Everything works fine and the cookie is written.
BUT... I only want the cookie to last 20 minutes so I've changed the time to 1200.
The instruction is now setcookie("CookieName", $CookieValue, time()+1200);
PROBLEM: The cookie isn't written anymore.

So my big question is: Why and can any of you wizzards out there help me?

To prevent any echo problems I'm using <?php ob_start(); ?> and <?php ob_end_flush(); ?> in the beginning and the end of the php-file.

TIA
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would imagine that the actual time sent to the browser is at or behind the time the browser thinks it currently is. If you have Firefox and the web developer extension, look at the response headers.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

How do you know the cookie isn't written anymore?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would guess by checking to see if it is set...

Code: Select all

<?php
if (isset($_COOKIE['CookieName'])) {
    // cookie is set
}
?>
Bitweiser
Forum Newbie
Posts: 11
Joined: Sun Jul 30, 2006 9:27 am

Post by Bitweiser »

tecktalkcm0391 wrote:How do you know the cookie isn't written anymore?
I use as said by Everah the Isset command. And when I look in the "cookie" folder at the clientside PC the cookie file isn't present.

You say "written anymore" but I say "Never written".
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do like Feyd suggested... Set your cookie as in your original code and then check the server time and check the client time. If the two are not the same, then that could explain your issue.
Bitweiser
Forum Newbie
Posts: 11
Joined: Sun Jul 30, 2006 9:27 am

Post by Bitweiser »

feyd wrote:I would imagine that the actual time sent to the browser is at or behind the time the browser thinks it currently is. If you have Firefox and the web developer extension, look at the response headers.
Sometimes the answer is just in front of your face but your to blind to see the solution. Indeed the timeindex on my server was wrong. I've reset the clock on the server and guess what....Now it works fine.

Thanks for your posts "feyd" and "Everah" you were both a great help 8)
Post Reply