<?php
$countfile = 'counter.txt';
$ipfile = 'ip.txt';
function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;
if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);
}
}
countint();
$value =file_get_contents($countfile);
?>
This is this the count.php function included along with two file ip.txt and counter.txt
the count is not going past 2 hits
after two hits it stops recording the ip address
Give this a try. I added an elseif
and in_array
condition elseif (in_array...
Sidenote: It makes sense that a unique
hit counter stops after 1
, otherwise it wouldn't be unique.
If you want to keep counting them, you can give this a go. If it doesn't work as expected, let me know and I will try and modify it, or delete the answer altogether.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$countfile = 'counter.txt';
$ipfile = 'ip.txt';
function countint(){
$ip = $_SERVER['REMOTE_ADDR'];
global $countfile , $ipfile;
if (!in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);
}
elseif (in_array($ip, file($ipfile, FILE_IGNORE_NEW_LINES))) {
$current = (file_exists($countfile)) ? file_get_contents($countfile) : 0;
file_put_contents($ipfile, $ip."\n", FILE_APPEND);
file_put_contents($countfile, ++$current);
}
}
countint();
$value =file_get_contents($countfile);
?>