开发者

How to set a cookie in php?

开发者 https://www.devze.com 2023-01-08 17:01 出处:网络
I set a cookie and then check if exist like this if(isset($_COOKIE[\"fan\"])) { //Do Nothing } else { $cookie = \"yes\";

I set a cookie and then check if exist like this

if(isset($_COOKIE["fan"]))
{
//Do Nothing
}
else
{
$cookie = "yes";
$expire=time()+60*60*24*30;
setcookie("fan", $cookie, $expire);
include_once("../inc/functions.php");
echo fan_page();    
}

When I test on my local machine, it works, but when i upload to productio开发者_开发百科n server, it doesn't work.

What am I doing wrong?

Thanks In Advance!

Marc


You probably need to set the domain for the cookie. Locally it defaults, but in production you may come across some issues if it's not set explicitly.

See the arguments for setcookie; http://www.php.net/manual/en/function.setcookie.php

I also suggest looking in your browser cache to see if it is being set.


A cookie set for one path/hostname may override a cookie set for another path/hostname even if it is newer.

For instance, if there is already a cookie set for "www.example.com" and you set one for "example.com", when you read back the same cookie you'll get the one that was set for "www.example.com".

Try setting the cookie for the more specific hostname.

This may be part of the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消