Simple Visitor Tracking System in PHP + MySQL-Free Download
Here’s a complete visitor tracking system in PHP + MySQL that logs the following fields:
- Visitor IP
- Country (via a free IP API)
- Date & Time
- Browser & Device
- Referrer Source (e.g., Facebook, Google, Direct)
- Unique ID
- Page Visited (logged on every page)
✅ 1. MySQL Table Structure
CREATE TABLE visitor_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
visitor_id VARCHAR(100),
ip_address VARCHAR(45),
country VARCHAR(100),
browser VARCHAR(255),
device VARCHAR(255),
referrer TEXT,
page_visited TEXT,
visit_time DATETIME
);
✅ 2. PHP Visitor Logger Script
Create a file: visitor_logger.php
<?php
function getUserIP() {
return $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';
}
function getBrowser() {
$agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($agent, 'Firefox') !== false) return "Firefox";
elseif (strpos($agent, 'Chrome') !== false) return "Chrome";
elseif (strpos($agent, 'Safari') !== false) return "Safari";
elseif (strpos($agent, 'MSIE') !== false || strpos($agent, 'Trident/7') !== false) return "Internet Explorer";
else return "Other";
}
function getDevice() {
$agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/mobile/i', $agent)) return "Mobile";
elseif (preg_match('/tablet/i', $agent)) return "Tablet";
else return "Desktop";
}
function getCountry($ip) {
$details = @json_decode(file_get_contents("http://ip-api.com/json/$ip"));
return $details && $details->status == 'success' ? $details->country : "Unknown";
}
function getReferrer() {
return $_SERVER['HTTP_REFERER'] ?? "Direct";
}
function generateVisitorID() {
return bin2hex(random_bytes(8));
}
function trackVisitor($pdo) {
$visitor_id = $_COOKIE['visitor_id'] ?? generateVisitorID();
if (!isset($_COOKIE['visitor_id'])) {
setcookie('visitor_id', $visitor_id, time() + (86400 * 30), "/");
}
$ip = getUserIP();
$country = getCountry($ip);
$browser = getBrowser();
$device = getDevice();
$referrer = getReferrer();
$page = $_SERVER['REQUEST_URI'];
$time = date('Y-m-d H:i:s');
$stmt = $pdo->prepare("INSERT INTO visitor_logs (visitor_id, ip_address, country, browser, device, referrer, page_visited, visit_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$visitor_id, $ip, $country, $browser, $device, $referrer, $page, $time]);
}
// DB Connection
$pdo = new PDO("mysql:host=localhost;dbname=your_db_name", "your_username", "your_password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
trackVisitor($pdo);
?>
✅ 3. Include Logger on Every Page
At the very top of every page (before any output):
<?php include_once "visitor_logger.php"; ?>
✅ Optional: View Logs (Admin Panel)
Create a simple admin.php
:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=your_db_name", "your_username", "your_password");
$logs = $pdo->query("SELECT * FROM visitor_logs ORDER BY visit_time DESC")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>Visitor Logs</title>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px; border: 1px solid #ccc; }
th { background: #f4f4f4; }
</style>
</head>
<body>
<h2>Visitor Logs</h2>
<table>
<thead>
<tr>
<th>ID</th><th>Visitor ID</th><th>IP</th><th>Country</th><th>Browser</th>
<th>Device</th><th>Referrer</th><th>Page</th><th>Date & Time</th>
</tr>
</thead>
<tbody>
<?php foreach ($logs as $log): ?>
<tr>
<td><?= $log['id'] ?></td>
<td><?= $log['visitor_id'] ?></td>
<td><?= $log['ip_address'] ?></td>
<td><?= $log['country'] ?></td>
<td><?= $log['browser'] ?></td>
<td><?= $log['device'] ?></td>
<td><?= $log['referrer'] ?></td>
<td><?= $log['page_visited'] ?></td>
<td><?= $log['visit_time'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
⚠ Notes:
- Make sure your hosting allows
file_get_contents()
for external API (or usecURL
if needed). - The
ip-api.com
service is free for non-commercial use with limits. For production, consider a paid IP geolocation API. - This script uses a cookie to track repeat visitors for basic session continuity.