简体   繁体   中英

Simple PHP Unique page hit counter stops after two counts?

 <?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);

?>

had to change the !in_array to if you want non-unique counter

<?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);

     ?>
<?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);

   ?>

works fine for unique ip address counter.................... if you to increment for every page view try the above answer with if else statement............

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM