如何计算我网站的唯一访问者数量?
目标:计算您网站的唯一访问者,考虑每个用户每天(或每周)只能访问一次。
解决方案(PHP代码):
<?php // Open database connection $conn = new mysqli('localhost', 'root', 'password', 'database'); // Get user's IP address $ip = $_SERVER['REMOTE_ADDR']; // Check if user already visited today (or in the last week) $sql = "SELECT * FROM visitors WHERE ip='$ip' AND last_visit >= DATE_SUB(NOW(), INTERVAL 1 DAY)"; $result = $conn->query($sql); // If user is a new visitor if ($result->num_rows == 0) { // Insert user's IP and current timestamp $sql = "INSERT INTO visitors (ip, last_visit) VALUES ('$ip', NOW())"; $conn->query($sql); // Increment total visitor count $sql = "UPDATE stats SET visits = visits + 1"; $conn->query($sql); } // Get total number of unique visitors $sql = "SELECT COUNT(*) AS total_visitors FROM visitors"; $result = $conn->query($sql); $total_visitors = $result->fetch_assoc()['total_visitors']; // Display the number of unique visitors echo "Total unique visitors: $total_visitors"; // Close database connection $conn->close(); ?>
说明:
替代方案资源:
以上是如何使用 PHP 和 MySQL 跟踪我网站的唯一访问者?的详细内容。更多信息请关注PHP中文网其他相关文章!