İçeriğe geç

PHP ile günlük ziyaretçi sayısı

Merhabalar bu yazımda sizlere güzel işinize yarayacak bir örnek kod paylaşacağım.

Sitenize günlük kaç kişi girdiğini sayfanızda gösterebilirsiniz,kod giren kullanıcıların iplerini txt e dosyasına kaydeder ve 24 saatte bir kere temizler.

<?php 

function ipController($user_ip,$ip_file){

$ccc = 0;

foreach (file($ip_file) as $file) {

if ($user_ip == trim($file)) {
    $ccc = 0; // 0 ise ip listede var
    break;
}else {
	$ccc++;
}
}

return $ccc;
}
 
   function writeHit(){ 

    if (ipController($_SERVER['REMOTE_ADDR'],'ip.txt') == 0) {
 	   return;
      }else {
  
  $dosya = fopen("hit.txt","r");
  $hit = fread($dosya, filesize("hit.txt"));
  fclose($dosya);

  $yaz = $hit + 1;

  $dos = fopen("hit.txt","w");
  fwrite($dos, $yaz);
  fclose($dos);

  $ipfile = fopen("ip.txt","a");
  fwrite($ipfile, $ip."\n");
  fclose($ipfile);

 }

  	$hour = getdate()['hours'];
     
     if ($hour == 24) {
        $hit_f = fopen("hit.txt","w");
        fwrite($hit_f, "0");
        fclose($hit_f);

        $ip_f = fopen("ip.txt","w");
        fwrite($ip_f, "");
        fclose($ip_f);
     } 
 
   }


function countVisitor($ip_file){

   $count = 0;

    foreach (file($ip_file) as $file) { 

     $count++;

    }

    return $count;
}

/*
KULLANIMI:

writeHit();
echo countVisitor("ip.txt"); //divlerin içine echo ile basabilirsiniz.

*/

 ?>

 

 

 

 

Kategori:PHP

Bu yazı yorumlara kapalı.

Copyright © 2022 Kenan Atmaca