簡體   English   中英

PHP cookie和時間

[英]PHP cookie and time

我正在嘗試第一次使用cookie,我認為我不理解這個或者我的代碼正在做一些循環的事情。 有人介意幫我回到正確的道路上嗎?

我一直在閱讀php.net的幫助,但我認為我現在的想法很難:/

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

沒有讓你的代碼安靜,但正如你評論的那樣

if(!isset($_COOKIE['popup_timer'])) {
   //Show popup
   setcookie("popup_timer", '', time()+3600);
}

只有在沒有設置$_COOKIE ,上面會拋出一個彈出窗口,一旦它拋出彈出窗口,就會設置一個設置為一小時到期的cookie。

您應該將當前時間與存儲在cookie中的時間進行比較,如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM