簡體   English   中英

如何檢查特定網頁上的上次訪問日期? 我想在該頁面上顯示 7 天后鏈接已過期

[英]How do I check last visited date on particular web page ? I want to show link expired after 7 days on that page

我想在該瀏覽器上存儲第一個訪問日期,7 天后我想在該網頁上顯示一些其他內容。 那么如何檢查該用戶第一次訪問網頁的時間呢? 我想讓該內容僅顯示 7 天。如何使用 php 代碼實現此目的? 請幫我解決一下這個。

根據建議,您可以使用:
餅干
設置cookie

例子:

// When a user visit that page, you will check whether a cookie is     
// set or not. If not, then you can set a cookie with the current time     
// as its value. If already set, then check if its time is greater than      
// 7 days or not.

$showContent = true;
$datetime = new \DateTime(); 
$now = $datetime->format('Y-m-d H:i:s'); //current datetime

//check cookie
if(!isset($_COOKIE['pageVisitTime'])) {
    setcookie("pageVisitTime", $now, time() + (10 * 365 * 24 * 60 * 60));  /* using a far future date */
} else {
    $pageVisitTime = $_COOKIE['pageVisitTime'];
    $visitDateTime = new \DateTime($pageVisitTime);
    if($visitDateTime->diff($datetime)->days > 7) {
       $showContent = false;
    }
}

//Depending on value of $showContent you can control your content

暫無
暫無

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

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