問題:
我需要一個可計算唯一訪客的計數器訪問我網站的訪客。所謂獨特,是指一個人每天或每週只能查看一次貼文。您能提供對應的 PHP 程式碼嗎?
答案:
以下PHP 程式碼將計算您網站的唯一訪客數量,限制每位訪客每天只能計數一次:
<?php // Initialize variables $filePath = 'visitor_counts.txt'; $timeLimit = 86400; // One day in seconds (24 * 60 * 60) // Get the visitor's IP address $ip = $_SERVER['REMOTE_ADDR']; // Read the visitor counts file $visitorCounts = file_get_contents($filePath); // Parse the visitor counts into an array $visitorCountsArray = explode("\n", $visitorCounts); // Check if the visitor's IP address is already in the array if (in_array($ip, $visitorCountsArray)) { // Visitor has already been counted today echo "Visitor has already been counted today"; } else { // Add the visitor's IP address to the array $visitorCountsArray[] = $ip; // Update the visitor counts file file_put_contents($filePath, implode("\n", $visitorCountsArray)); // Increment the visitor count $visitorCount++; } // Echo the visitor count echo "Visitor count: $visitorCount"; ?>
說明:
以上是如何建立 PHP 腳本來計算每日限制的唯一網站訪客?的詳細內容。更多資訊請關注PHP中文網其他相關文章!