Inspiration For Creative Minds
Home » Labs » How to Make a Simple PHP Hit Counter for Website  

Filed under Labs

Are you looking for a Counter Script for you PHP project, then try the following one. It uses only some PHP code with a text file to build a Hit Counter. Make sure that your “counter.txt” is writable, other wise go to your cpanel and give 777 permission to it.

<?php 
 
$visitor_ip = $HTTP_COOKIE_VARS["user_ip"]; 
$counter = "counter.txt"; 
$counter_file_line = file($counter); 
 
if(!$vistor_ip) 
{ 
  setcookie("user_ip", $REMOTE_ADDR, time()+360000); 
  $counter_file_line[0]++; 
  $cf = fopen($counter, "w+"); 
  fputs($cf, "$counter_file_line[0]"); 
  fclose($cf); 
} 
elseif($vistor_ip != $REMOTE_ADDR) 
{ 
  $counter_file_line[0]++; 
  $cf = fopen($counter, "w+"); 
  fputs($cf, "$counter_file_line[0]"); 
  fclose($cf); 
} 
?>
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook
Labs

Posts you may like: