我找到了一个脚本,可以让我显示唯一身份访问者的数量,但似乎有问题。在它停止之前我数了三次。我不擅长PHP,但我试图弄清楚问题,但不能。我的代码中有什么问题吗?还有另一种更好,更简单的方法吗?
<?php
$filename = "UniqueCount.txt";
if (!file_exists($filename))
{
// hits.txt doesn't exist, let's try to create it.
$fd = fopen($filename, "w+");
fclose($fd);
}
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
// Print out the number of unique visitors we have had.
echo $hits;
$fd = fopen($filename, "r");
$fstring = fread($fd, filesize($filename));
fclose($fd);
$fd = fopen($filename, "w");
$fcounted = $fstring . "
" . $_SERVER["REMOTE_ADDR"];
$fout = fwrite($fd, $fcounted);
fclose($fd);
?>
要在启动时创建文件。我得到并且错误,说
0
Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/46/10721146/html/UniqueHits.php on line 20
提前致谢,
Elite Gamer
答案 0 :(得分:1)
您可以更轻松,更完美地将数据存储在数据库中,使其更好,更易于访问
创建两个表
CREATE TABLE `pageview` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page` text NOT NULL,
`userip` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
// totalview
CREATE TABLE `totalview` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page` text NOT NULL,
`totalvisit` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
插入和更新表
$check_ip = mysql_query("select userip from pageview where page='yourpage' and userip='$user_ip'");
if(mysql_num_rows($check_ip)>=1)
{
}
else
{
$insertview = mysql_query("insert into pageview values('','yourpage','$user_ip')");
$updateview = mysql_query("update totalview set totalvisit = totalvisit+1 where page='yourpage' ");
}
在此完成教程http://talkerscode.com/webtricks/create-a-simple-pageviews-counter-using-php-and-mysql.php