Bagaimanakah Saya Mengira Pelawat Unik ke Tapak Saya?
Matlamat: Kira pelawat unik ke tapak anda, mempertimbangkan hanya satu lawatan bagi setiap pengguna setiap hari (atau minggu).
Penyelesaian (Kod 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(); ?>
Penjelasan:
Sumber Alternatif:
Atas ialah kandungan terperinci Bagaimanakah Saya Boleh Menjejaki Pelawat Unik ke Laman Web Saya dengan PHP dan MySQL?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!