Reputation: 7293
I have found a script that will allow me to display the number of unique visitors, but there seems to be a problem. It counts me three times before it stops. I am not good in PHP, but i tried to figure out the problem, but could not. Is there something wrong in my code? Is there another better, easier way to do this?
<?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);
?>
To boot, when it creates the file. I get and error, saying
0
Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/content/46/10721146/html/UniqueHits.php on line 20
Thanks in advance,
Elite Gamer
Upvotes: 0
Views: 978
Reputation: 61
there is easier and more perfect way you have to store data in database its good and more accessible
create two table
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
the insert and update tables
$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' ");
}
complete tutorial here http://talkerscode.com/webtricks/create-a-simple-pageviews-counter-using-php-and-mysql.php
Upvotes: 1