PHP cookie和时间
I'm am experimenting with cookies for the first time and I don't think I'm understanding this or my code is doing something loopy. Someone mind helping me get back on the right path?
I keep re reading the php.net help but i think my mind is mush now :/
$currentTime = strtotime("now");
$popup_exp = strtotime("+1 hour");
if (!isset($_COOKIE['popup_timer'])) : //does cookie exists? if not, make it
setcookie("popup_timer", $currentTime);
endif;
if( ($popup_exp > $_COOKIE['popup_timer']) ):
//show my popup
endif;
我正在尝试第一次使用cookies,我不认为我理解这个或我的 代码正在做一些循环的事情。 有人介意帮助我回到正确的道路上吗? p>
我一直在阅读php.net的帮助,但我认为我现在的想法很简单:/ p>
$ currentTime = strtotime(“now”);
$ popup_exp = strtotime(“+ 1小时”);
if(!isset($ _ COOKIE ['popup_timer'])):// cookie存在吗? 如果没有,请将其设为
setcookie(“popup_timer”,$ currentTime);
endif;
if(($ popup_exp> $ _COOKIE ['popup_timer'])):
//显示我的popup
endif;
code> pre>
div>
Not getting your code quiet well, but as you commented
if(!isset($_COOKIE['popup_timer'])) {
//Show popup
setcookie("popup_timer", '', time()+3600);
}
The above will throw a pop up only if the $_COOKIE
is not set, once it throws the pop up, a cookie will be set with an expiration set to an hour.
You should compare your current time with the time stored in the cookie, like this
$currentTime = strtotime('now');
if (!isset($_COOKIE['popup_timer'])) {
setcookie('popup_timer', $currentTime);
} else {
if ($currentTime > $_COOKIE['popup_timer'] + 60 * 60) {
// If an hour has passed since cookie creation
// show your popup
}
}