| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd_/ |
Upload File : |
<?php
/**
* track-visitor.php
* Catat kunjungan halaman index — Simple Version
*/
// Pastikan koneksi database tersedia
if (!isset($conn) || !$conn) {
return; // jangan lanjut kalau tidak ada koneksi
}
// Hanya jalan di halaman index
$uri = $_SERVER['REQUEST_URI'] ?? '';
$is_index = (
$uri === '/' ||
$uri === '/index.php' ||
strpos($uri, '/index.php?') === 0
);
if (!$is_index) {
return;
}
// =============================
// Filter Bot
// =============================
$ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
$bots = ['bot', 'crawler', 'spider', 'curl', 'wget', 'python', 'nikto', 'sqlmap'];
foreach ($bots as $b) {
if (strpos($ua, $b) !== false) {
return;
}
}
// =============================
// Deteksi Visitor Baru / Lama
// =============================
$cookie_name = 'bpkd_visitor';
$visitor_type = 'new';
$visitor_token = '';
if (!empty($_COOKIE[$cookie_name])) {
$visitor_type = 'returning';
$visitor_token = $_COOKIE[$cookie_name];
} else {
$visitor_token = bin2hex(random_bytes(16));
setcookie($cookie_name, $visitor_token, [
'expires' => time() + (86400 * 365),
'path' => '/',
'secure' => isset($_SERVER['HTTPS']),
'httponly' => true,
'samesite' => 'Lax'
]);
}
// =============================
// Simpan ke Database
// =============================
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
$today = date('Y-m-d');
$now = date('H:i:s');
$page = '/index.php';
$stmt = $conn->prepare("
INSERT INTO visitors
(ip_address, user_agent, page_visited, visit_date, visit_time, visitor_type, visitor_token)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
if ($stmt) {
$stmt->bind_param("sssssss",
$ip, $ua, $page, $today, $now, $visitor_type, $visitor_token
);
$stmt->execute();
$stmt->close();
}