0

我找到了一个脚本,可以让我显示唯一身份访问者的数量,但似乎有问题。它在停止之前数了我三遍。我不擅长 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

提前致谢,

精英玩家

4

1 回答 1

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

// 全视图

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

于 2016-03-08T15:34:38.357 回答