Simple PHP Hit Counter

By MartyniP on Aug 30, 2010

A simple PHP hit counter which tracks the amount of times the page is viewed, Then they are stored in a txt file. The file hits.txt must be writable else

<?php
// Copyright MartyniP.co.uk
// MartyniP Martyni Productions

// First we will read the hits.txt file to find out the current number of hits
$file = "hits.txt";
$fh = fopen($file,"r");
$hits = fread($fh, 5);
fclose($fh);

// Adds 1 to the $hits
$hits++;

// Write the hits.txt file with 1 more than before
$fh = fopen($file, 'w');
fwrite($fh, $hits);
fclose($fh);

// echo out the amount of hits the sute has gotten
echo $hits;

?>

Comments

Sign in to comment.
MartyniP   -  Sep 25, 2010

jhine, yes i have, just i did not use them in this code

 Respond  
jhine   -  Sep 24, 2010

Have you taken a look at file_get_contents and file_put_contents?

 Respond  
DarkGamer120   -  Sep 15, 2010

nice

 Respond  
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.