<!DOCTYPE html>
<html>
<head>
	<title>Visitor Log</title>
	<style>
		table {
			border-collapse: collapse;
			width: 100%;
		}
		th, td {
			text-align: left;
			padding: 8px;
			border-bottom: 1px solid #ddd;
		}
		tr:hover {
			background-color: #f5f5f5;
		}
		th {
			background-color: #4CAF50;
			color: white;
		}
	</style>
</head>
<body>
<?php
// Connect to MySQL database
$servername = 'localhost';
$username = 'root';
$password = '123456';
$dbname = 'visitors';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
}

// Insert visitor information into database
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
	$ip = $_SERVER['REMOTE_ADDR'];
}
$date = date('Y-m-d H:i:s');
$sql = 'INSERT INTO visitors (ip, date) VALUES (?, ?)';
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $ip, $date);
$stmt->execute();

// Display visitor log
$sql = 'SELECT * FROM visitors';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
	echo '<table>
';
	echo '<tr><th>ID</th><th>IP</th><th>Date</th></tr>
';
	while ($row = $result->fetch_assoc()) {
		echo '<tr><td>' . $row['id'] . '</td><td>' . $row['ip'] . '</td><td>' . $row['date'] . '</td></tr>
';
	}
	echo '</table>
';
} else {
	echo 'No visitors yet.';
}

$conn->close();
?>
</body>
</html>
Visitor Log: Track Website Traffic with PHP and MySQL

原文地址: https://www.cveoy.top/t/topic/l6Lj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录