Chamelion wrote:Hello, I am working on a counter
which will record unique page views
so that users can see how many times
an advert has been visited like on ebay.
Is this method the best way to do this, or is there a quicker, more efficient way that I have overlooked?
Thanks in advance.
Hello.
Me and a friend created a such script.
to only count Unique IP once and never again.
This is not really a hits counter,
because them normal Counters can count same IP,
if coming back after some time
( count again after 24 hours, or even 2 hours later or each new session ).
We can call this script individual counter
or more precise - Unique IP Visitors, perhaps.
for example:
If you write an article and put this article in one separate page
and use this counter,
you will see HOW MANY PEOPLE have read your article.
At least how many did visit your article.
Here is my script:
<?php
// Uncomment For Debug only!
// error_reporting(E_ALL);
$ipfile="data/iplist.php"; // PHP protected file, to write IP-numbers to
$ip=$_SERVER[ 'REMOTE_ADDR' ]; // simple IP detector
$view=0; // if to Display Hits numbers result, see end of this script
if(isset($_GET["view"]) && $_GET["view"]=="true")
$view=1;
if(!is_file($ipfile) || filesize($ipfile)<22)
{
$iplist=fopen($ipfile,"wb");
fwrite($iplist,"<?php exit('no!'); ?>\n");
fclose($iplist);
$data="";
}else{
$iplist=fopen($ipfile,"rb");
$data=fread($iplist,filesize($ipfile));
fclose($iplist);
$data=str_replace("<?php exit('no!'); ?>\n","",$data);
}
$visits=0;
$found=0;
if(!empty($data)){
$ips=explode("\n",rtrim($data));
$visits=count($ips);
foreach($ips as $individual)
{
if($ip==$individual)
$found=1;
}
}
if(!$found)
{
$iplist=fopen($ipfile,"ab");
fwrite($iplist,$ip."\n");
fclose($iplist);
$visits++;
}
if($view)
print "Unique Visits: ".$visits;
?>
I think you can figure out how it works.
settings:
$ipfile="data/iplist.php"; // file to write IP-numbers to
$ip=$_SERVER[ 'REMOTE_ADDR' ]; // simple IP detectorr
$view=0; // if to Display Hits numbers result, see end of this script
To use you include in your index.php
for example, add this line:
include "ipcounter.php";
Then if you call your "index.php" or directly the "ipcounter.php"
with:
http://yourURL/ipcounter.php?view=true
.... you will get A DISPLAY of the result, of total number of unique hits.
This is total number of IP-addresses stored in the 'iplist.php'
Every IP number is written in a new line, in this logfile:
12.12.13.145
32.231.57.83
212.87.77.3
etc.
etc.
You will of course also be able to use 'iplist.php'
for tracking who visited your page.
And maybe also of some help if necessary to BAN some BAD HACKER 😃
You can also find my script online in my web server PUBLIC FOLDER.
alnog with some other small PHP Scripts I have written:
http://okay.mine.nu/pub/
http://okay.mine.nu/pub/ipcounter1a.phps
Regards
halojoy
🙂