24小时访问者记录 - 实时监控网站访问
<!DOCTYPE html>
<html>
<head>
<title>24小时访问者记录</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 ('$ip', '$date')';
$conn->query($sql);
// Display visitor log
$sql = 'SELECT * FROM visitors WHERE date >= DATE_SUB(NOW(), INTERVAL 24 HOUR)';
$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 in the last 24 hours.';
}
$conn->close();
?>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/l6LJ 著作权归作者所有。请勿转载和采集!