Forum Moderators: coopster
I am writing a php counter, so far the counter simple ads one to its number each time the page is refreshed, this is done from a mysql database, I was wondering how could I make this more realistic, i.e. stop it from adding one each time the same user hits refresh, I would like the count to represent a number as close to real unique hits.
I was thinking about using $_SERVER['REMOTE_HOST']; and storing a list of host names that had visited the site but then what would happen if the same user came back days later and had the same hostname, that should warrant an extra count but it wouldn't because I would have stored there hostname and prevented a count from being done, so can anyone guide me in the right direction? the code I current have is below.
<?php
require_once('/home/www/juttuffi/dbc.php');
$check = mysql_query("SELECT * FROM tcounter LIMIT 0,1");
if(!$check)
{
$create = mysql_query("CREATE TABLE tcounter('id INT NOTNULL AUTO_INCREMENT, '.
'count BIGINT NOT NULL, '.
'PRIMARY KEY(id)')");
}
else
{
$read = mysql_query("SELECT * FROM tcounter");
$count = mysql_fetch_array($read,MYSQL_ASSOC);
$tmp = $count['count'];
$tmp++;
$countadd = $tmp;
$write = mysql_query("UPDATE tcounter SET count='$countadd'");
$countprint = mysql_query("SELECT * FROM tcounter");
$cprint = mysql_fetch_array($countprint,MYSQL_ASSOC);
$cval = $cprint['count'];
echo $cval;
}
mysql_close($dbc);
?>
I have been looking at this code from php.net in a way of improving my counter.
<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $hostname;
?>
any help would be appreciated, :-) Thanks
Chris
table description would be like this...
=========================
table name: logs
id bigint(20) unsigned not null auto_increment primary key
ip varchar(100)
visited varchar(100)
table name: tcounter
counter int(9)
=========================
<?php
require_once('/home/www/juttuffi/dbc.php');
$maxCache = 10;
$ipVisitor = $_SERVER["REMOTE_ADDR"];
$result = mysql_query("select id from logs where ip!= ".$ipVisitor." limit ".$maxCache);
$exist = mysql_num_rows($result);
if ($exist > 0)
{
mysql_query("insert into logs (ip,visited) values ('".$ipVisitor."','".mktime()."')");
}
$result = mysql_query("select count(id) from logs");
$counterVisitor = mysql_fetch_rows($result);
echo $counterVisitor[0];
?>