如何使用php计算我的网页的唯一访客,并每天(24小时)重新计数?
此外,我需要将访客数量保存在文本文件中。我不需要使用数据库。
这是我尝试的和它的示例代码,以获得用户ip和计数+1:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$count = file_get_contents("counter.txt");
$count = trim($count);
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
echo $ip;
?>
<?php
$filename = date("Ymd") . "_counter.txt";
$seenFilename = date("Ymd") . '_seen_ip.txt';
$ips = array();
if (file_exists($seenFilename))
{
$ips = file($seenFilename);
$ips = array_map('trim', $ips);
}
if(!in_array($_SERVER['REMOTE_ADDR'], $ips))
{
$visits = 0;
if (file_exists($filename)) {
$visits = file_get_contents($filename);
}
file_put_contents($filename, ++$visits);
$data = $_SERVER['REMOTE_ADDR'] . PHP_EOL;
$fp = fopen($seenFilename, 'a');
fwrite($fp, $data);
}
?>
此代码将每天创建一个新文件,并记录每次唯一访问的计数。