Cookie doesn't set on server

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
Mr X
Forum Newbie
Posts: 5
Joined: Thu Nov 03, 2011 8:49 am

Cookie doesn't set on server

Post by Mr X »

Hello to all. Yesterday I came across an issue where I can't execute cookie set properly on the server. It works locally though without no problems. The domain's path is "/tmp" for session_path. Here's the code. Thanks for comments:

Code: Select all


if (!isset($_COOKIE['TestCookie'])) { 
$vote_com2 =  mysqli_query($connect,"UPDATE news SET up = '".$upv."' WHERE news_id = '$idn'" );
setcookie("TestCookie", $idn, time()+3600,"/",".mydomain.net"); 
}

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Cookie doesn't set on server

Post by pickle »

This may be obvious, but is the code being run on a subdomain of "mydomain.net" (which I assume is just a dummy domain for the purposes of posting here)?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Mr X
Forum Newbie
Posts: 5
Joined: Thu Nov 03, 2011 8:49 am

Re: Cookie doesn't set on server

Post by Mr X »

Hi - yes it's a dummy domain. The cookie runs on the main domain i.e. ( not on subdomain ). For now I've referred to using $_SESSION instead - helps for as long as the browser is on. But cookie is the proper way for this feature..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Cookie doesn't set on server

Post by pickle »

Is the time() correct? If time() on the server is an hour or more behind your computer's clock, it would be invalid.

There's an extension you can get for Firefox that captures http headers (which is what is used to set cookies). You can use that to check if the proper headers are being sent to your computer.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Mr X
Forum Newbie
Posts: 5
Joined: Thu Nov 03, 2011 8:49 am

Re: Cookie doesn't set on server

Post by Mr X »

I see - I'm gonna check the time out and I'll mind the extension. Thanks a lot. Speak soon..
Mr X
Forum Newbie
Posts: 5
Joined: Thu Nov 03, 2011 8:49 am

Re: Cookie doesn't set on server

Post by Mr X »

The time of the server is Chicago IL that is about 4h difference between my country...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Cookie doesn't set on server

Post by pickle »

So your best bet is to use $_SESSION, or have a cookie with a longer expiry, or set it to 0, which kills the cookie when the browser quits.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Mr X
Forum Newbie
Posts: 5
Joined: Thu Nov 03, 2011 8:49 am

Re: Cookie doesn't set on server

Post by Mr X »

Sure - now I now what the issue is about - thanks a lot !
Post Reply