Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Hits Counter
In this tutorial we'll learn how to make a simple text based hits counter using PHP.

For this script we'll be using a text file to store the hits count, when the user visits the page it will call our counter script. It will take the count from the file, increase it by one and store it back in the file. Then it will print the amount of hits on the page. Let's look at the code, then we'll go over it.

<?php
    $file 
'counter.txt';

    if(!
file_exists($file))
    {
        
$handle fopen($file'w');
        
fwrite($handle0);
        
fclose($handle);
    }

    
$count file_get_contents($file);
    
$count++;

    if(
is_writable($file))
    {
        
$handle fopen($file'w+');
        
fwrite($handle$count);
        
fclose($handle);
    }
    else
    {
        echo 
'Could not increment the counter!<br />';
    }

    echo 
number_format($count).' Hits';
?>

The first thing we have, of course, is the opening php tag. Followed by the declaration of a variable. ($file) This variable holds the path to our counter file, that will remember how many hits we've recieved.

The next block of code is an if statement that uses the function
file_exists() to find out if the file held in the variable $file does not exist. (Note the exclamation point in front of the function makes it check if the output returned by the function is false)

If it doesn't exist, the script will create the file using fopen() in 'w' mode. It will then use fwrite to write '0' to the file. Then it will close the file handle, using fclose().

Next we use file_get_contents() to grab our current hit count, then we increment it by one.

The next chunk of code will check to make sure the file is writable using is_writable(). If it is writable, the script will open the file and write the incremented hit count to the file, then close it. If it is not writable, it will display an error message using echo().

Finally, we use number_format() to make the number more easy to read and print the count to the page.

That's it, you've got a fully functional hits counter. I hope this tutorial helped you. If you have any questions use the "Discuss this tutorial" link in the right column. Smile

Discuss Tutorial: Hits Counter 13 Comments
Comment by Ron on Mar 16, 2004, 4:31 am
Please post questions or comments about this tutorial below. Smile
Comment by Blade on Apr 26, 2004, 12:44 am
Nice, I'm understanding more and more Grin
Comment by Ron on Apr 28, 2004, 1:31 am
Glad to hear it Smile
Comment by jorda on Jun 6, 2004, 1:20 am
does that code posted work.
does it show the stats for hides them. please email me
Comment by jin on Nov 11, 2004, 8:04 am
how can i make the counter.txt writable?
chmod?
Comment by Austin on Nov 22, 2004, 1:13 am
How do you make the hits visable? Please e-mial me.
Comment by mikkeey on Jan 4, 2005, 10:20 pm
Would you not have to CHMOD "counter.txt" 777?
Comment by Nanyo on Jan 23, 2005, 9:27 pm
Yes, you need to CHMOD the text file to 0777 IF your running this under unix. Windows you don't have to. Wink
Comment by Samiwerty on Apr 2, 2005, 3:19 pm
Ehh.. I have one problem.. The script works. But how i can get it so it starts from number 1873
Comment by fedekiller on Jul 8, 2005, 6:58 pm
really goooooooood! i undestand all and now i know how to make tyhat alone, thank ^_^ Laughing Grin Wink

« Previous [ 1 2 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.