Complex counter

By log2 on Dec 20, 2004

This is a more complex counter than the last one I wrote, this one will tell you any error and it will tell you when it's been updated, all you have to do is make a file counter.txt

<?php 
// the name of the counter file 
$filename = "counter.txt"; 

// get the contents of the file 
$views = intval(file_get_contents($filename)); 

$views++; 

echo "This page has been viewed ". $views ." times."; 

// check if the file exists, 
// and can be written to. 
if (is_writable($filename)) 
{ 
   // open the file for writing ('w') 
   if ($fp = fopen($filename, 'w')) 
   { 
       // write to the file. Put "1" in it. 
       if (fwrite($fp, $views) !== FALSE) 
       { 
           // you can remove this line 
           echo "Counter was updated!"; 
            // close the file 
            fclose($fp); 
       } 
       else 
       { 
           echo "Could not write to file $filename"; 
       } 
   } 
   else 
   { 
       echo "Could not open file $filename for writing."; 
   }                 
} 
else 
{ 
   echo "The file $filename is not writable"; 
} 
?> 

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.