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;
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
}
}