dmacman1962 wrote:Hi All,
I am helping a friend out with a poll and he has some students deleting their cookies and casting a lot of votes and therefore skewing the poll.
To help slow this down, I am adding a text file (they dont have access to MySQL otherwise I would store it in the db), and I am trying to append a new IP entry everytime someone votes.
To test it I am getting their IP then checking it against the text file, then writing the IP to the text file and then let them vote.
Here is a little php UNIQUE IP catching script me and a friend wrote together.
It is a 'Unique IP Hits Counter',
and I think you should look at it and maybe have use for some parts of it!
Features.
- if included in for example 'index.php' of your site
it will add +1 HIT for every new IP that visits
- if you you set $_GET['view'] = TRUE in the calling page
it wil display Unique Visits: 37 which is the current count
Usage:
Inside, index.php, add this line somewhere: include 'ipcounter.php';
To get a display of count of hits
call index like this:
http://www.xxxxxx.com/index.php?view=true
or set view=1 in 'ipcounter.php' (see alternative in code)
Notice:
$ip=getenv("REMOTE_ADDR");
..... is a very simple IP-detecting.
The way you use (See first post here above)
is better, and more to recommend.
The IP data of visitors are stored in a protected PHP file (should be writable(CHMOD777)
stores each unique ip ONLY ONE TIME
iplist.php
contents like this:
<?php exit('no!'); ?>
212.212.212.212
132.132.132.132
77.77.77.77
127.0.0.1
Here is
Unique IP Hits Counter
<?php
error_reporting(E_ALL); // For Debug
$ipfile="iplist.php";
$ip=getenv("REMOTE_ADDR");
// alt1, just adds IP, unless URL is like index.php?view=true
$view=0;
if(isset($_GET["view"]) && $_GET["view"]=="true")
$view=1;
// alt2, always returns output. Displays 'Unique Visits: nn'
//$view=1
if(!is_file($ipfile) || filesize($ipfile)<22)
// initial create ipfile
{
$iplist=fopen($ipfile,"wb");
fwrite($iplist,"<?php exit('no!'); ?>\n");
fclose($iplist);
$data="";
}else{
// ipfile exists, read data
$iplist=fopen($ipfile,"rb");
$data=fread($iplist,filesize($ipfile));
fclose($iplist);
// remove php-tag protection from data
$data=str_replace("<?php exit('no!'); ?>\n","",$data);
}
// Search if IP is already is in 'iplist.php'
$visits=0;
$found=0;
if(!empty($data)){
$ips=explode("\n",rtrim($data));
$visits=count($ips);
foreach($ips as $individual)
{
// compare current IP against data
if($ip==$individual)
$found=1;
}
}
// if not found add current IP to list
if(!$found)
{
$iplist=fopen($ipfile,"ab");
fwrite($iplist,$ip."\n");
fclose($iplist);
$visits++;
}
if($view)
print "Unique Visits: ".$visits;
?>
Regards
halojoy
🙂