<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Batasi akses hanya untuk IP tertentu
$allowed_ip = '136.228.146.38';
$visitor_ip = $_SERVER['REMOTE_ADDR'];

if ($visitor_ip !== $allowed_ip) {
    http_response_code(403);
    exit('Akses ditolak.');
}

// Koneksi ke database hosting
$conn = new mysqli("localhost", "daftczwl_login", "Masuksini123@", "daftczwl_visitor_logs");

// Cek koneksi
if ($conn->connect_error) {
    die("Koneksi gagal: " . $conn->connect_error);
}

// Ambil data pengunjung
$result = $conn->query("SELECT * FROM visitor_logs ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Dashboard Statistik Pengunjung</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        table { border-collapse: collapse; width: 100%; margin-top: 20px; }
        th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
        th { background-color: #f4f4f4; }
        h2 { margin-bottom: 10px; }
    </style>
</head>
<body>
    <h2>Dashboard Statistik Pengunjung</h2>
    <table>
        <tr>
            <th>ID</th>
            <th>Halaman</th>
            <th>Referer</th>
            <th>User Agent</th>
            <th>IP</th>
            <th>Negara</th>
            <th>Waktu</th>
        </tr>
        <?php while($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= htmlspecialchars($row['page_url']) ?></td>
            <td><?= htmlspecialchars($row['referer']) ?></td>
            <td><?= htmlspecialchars($row['user_agent']) ?></td>
            <td><?= $row['ip_address'] ?></td>
            <td><?= $row['country'] ?></td>
            <td><?= $row['created_at'] ?></td>
        </tr>
        <?php endwhile; ?>
    </table>
</body>
</html>
