本文目录导读:

我来为你详细介绍几种 PHP 计数器的实现方法:
最简单的文本文件计数器
<?php
// counter.php
$counterFile = "counter.txt";
// 检查文件是否存在,不存在则创建
if (!file_exists($counterFile)) {
$counter = 1;
file_put_contents($counterFile, $counter);
} else {
// 读取当前计数
$counter = file_get_contents($counterFile);
$counter++;
file_put_contents($counterFile, $counter);
}
echo "您是第 " . $counter . " 位访客!";
?>
带IP去重的计数器
<?php
// unique_counter.php
$counterFile = "counter.txt";
$ipFile = "ip_list.txt";
// 获取访客IP
$visitorIP = $_SERVER['REMOTE_ADDR'];
// 读取已记录的IP列表
$ipList = file_exists($ipFile) ? file($ipFile, FILE_IGNORE_NEW_LINES) : [];
// 检查是否为新访客
if (!in_array($visitorIP, $ipList)) {
// 添加新IP
$ipList[] = $visitorIP;
file_put_contents($ipFile, implode("\n", $ipList));
// 更新计数器
if (file_exists($counterFile)) {
$counter = file_get_contents($counterFile);
$counter++;
} else {
$counter = 1;
}
file_put_contents($counterFile, $counter);
} else {
// 读取当前计数
$counter = file_exists($counterFile) ? file_get_contents($counterFile) : 0;
}
echo "您是第 " . $counter . " 位访客!";
?>
使用MySQL数据库的计数器
<?php
// mysql_counter.php
// 数据库配置
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';
// 连接数据库
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 创建表(如果不存在)
$sql = "CREATE TABLE IF NOT EXISTS page_counter (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
page VARCHAR(255) NOT NULL,
count INT(10) UNSIGNED DEFAULT 0
)";
$conn->query($sql);
// 当前页面
$currentPage = $_SERVER['REQUEST_URI'];
// 更新计数器
$sql = "INSERT INTO page_counter (page, count) VALUES ('$currentPage', 1)
ON DUPLICATE KEY UPDATE count = count + 1";
$conn->query($sql);
// 读取计数
$sql = "SELECT count FROM page_counter WHERE page = '$currentPage'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$count = $row['count'];
} else {
$count = 0;
}
echo "本页访问量:$count 次";
$conn->close();
?>
图片计数器(显示数字图片)
<?php
// image_counter.php
header('Content-Type: image/png');
// 读取计数
$counterFile = "counter.txt";
if (file_exists($counterFile)) {
$counter = file_get_contents($counterFile);
$counter++;
} else {
$counter = 1;
}
file_put_contents($counterFile, $counter);
// 数字图片路径(需要准备0-9的数字图片)
$digitImages = [
'0' => 'digits/0.png',
'1' => 'digits/1.png',
'2' => 'digits/2.png',
// ... 以此类推
];
// 创建新图片
$counterStr = sprintf("%06d", $counter);
$width = strlen($counterStr) * 20;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
$x = 0;
for ($i = 0; $i < strlen($counterStr); $i++) {
$digit = $counterStr[$i];
if (isset($digitImages[$digit]) && file_exists($digitImages[$digit])) {
$digitImg = imagecreatefrompng($digitImages[$digit]);
imagecopy($image, $digitImg, $x, 0, 0, 0, 20, 30);
imagedestroy($digitImg);
}
$x += 20;
}
imagepng($image);
imagedestroy($image);
?>
使用缓存的计数器(提高性能)
<?php
// cached_counter.php
$counterFile = "counter.txt";
$cacheFile = "counter_cache.txt";
$cacheTime = 60; // 缓存时间(秒)
// 检查缓存
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cacheTime)) {
$counter = file_get_contents($cacheFile);
} else {
// 读取实际计数
if (file_exists($counterFile)) {
$counter = file_get_contents($counterFile);
$counter++;
file_put_contents($counterFile, $counter);
// 更新缓存
file_put_contents($cacheFile, $counter);
} else {
$counter = 1;
file_put_contents($counterFile, $counter);
file_put_contents($cacheFile, $counter);
}
}
echo "您是第 " . $counter . " 位访客!";
?>
使用方法
- 文本文件计数器:创建
counter.txt文件并设置权限为 666 - MySQL计数器:确保数据库已连接,创建相应数据表
- 图片计数器:准备数字图片,创建图片路径
注意事项
- 文件权限:确保 PHP 有读写文件的权限
- 并发问题:高并发场景建议使用数据库或Redis
- 安全性:防止直接访问计数器文件
- 性能:大量访问时考虑使用缓存
根据你的需求选择合适的实现方式,最简单的文件计数器适合小型网站,数据库计数器适合需要精确统计的场景。