1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?php
namespace App\Service;
class StatService
{
public function getStats()
{
$ip=fopen('last_ip.txt', 'c+');
$check=fgets($ip);
$file=fopen('counter.txt', 'c+');
$count=intval(fgets($file));
//si l'ip du dernier visiteur est différent on incrémente de 1
if($_SERVER['REMOTE_ADDR'] !=$check){
fclose($ip);
//w+ = ecrase les données dans le fichier
$ip=fopen('last_ip.txt', 'w+');
fputs($ip, $_SERVER['REMOTE_ADDR']);
$count++;
fseek($file,0);
fputs($file,$count);
}
fclose($file);
fclose($ip);
echo ($count);
}
} |