Web Development

How to make a Hit Counter with PHP

PHP Hit Counter

In this lesson we are going to make a very simple hitcounter.

A hitcounter is a script, that counts the number of visitors to your site.

You actually have 2 kinds of hitcounter:

  1. visable hitcounters ( every visitor can see the number of “hits” )

  2. “invisable” hitcounters ( only visable to you, the webmaster)

In this lesson we are going to make a simple visible hitcounter .

Who can learn PHP (and follow this tutorial) ?

Well, to be short everyone can.

But if you don’t know anything about HTML, it is going to be very difficult.

HTML is a must for every webdesigner/webdeveloper , so you should learn HTML first, if you don’t know it already.

I expect you to be quite familiar with Internet and WWW

What should the hitcounter do?

Every time a surfer on the Internet visits your page the number of hits should increase by one.

Sounds simple, not?

Well, actually, it is simple too.

Start coding

First, choose the page , that you want to count the hits for . The best choice is to take your intropage of your homepage, probably called index.html of index.htm

Just rename this file to index.php. Now we make another file called counter.txt ,
just leave this file “counter.txt” completely empty.

Now for the real script:

open the file “index.php” in a simple text editor like Notepad

Place all the PHP code we are going to write now on the place in your HTML code , where you want your counter to appear.

first we will start with the opening and closing tags of PHP.

The opening tag for PHP looks like this:

<?php

and the closing tag is:

?>

Everything between those tags is considered actual PHP code by the PHP interpreter on the webserver.

Now, we are going to make our first variable.

Type this:

$filename= "counter.txt" ;

This means that now there is a variable called “filename” that has the value “counter.txt”.

Don’t forget the semi-colon ( ; ) , you should end every line of code with this character, or else the PHP interpreter will give you an error.

Now we are going to write the code, that is going to show the number of hits until now:

Type this:

$fd = fopen ($filename , "r") or die ("Can’t open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;

This is what this code does:

the function “fopen” opens the file “counter.txt” in reading mode ( “r”).

the function “fread” reads the contents of the file we just opened and assigns this content to the variable called ”$fstring”.

The next line:

echo “$fstring” means that the contents of this variable is shown on your monitor.

Now we are going to write the “counting” part of the script.

$fd = fopen ($filename , "w") or die ("Can’t open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;

Here we again open the file called “counter.txt” , but now in writing mode.

We ‘ll use the variable $fstring again and increment it with one.

Then this value is written to the counter.txt file (fwrite) and the file is closed (fclose).

This is the complete script:


$filename= "counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;

?>

If you made no typo’s , save this file as “index.php”.

If you now upload this file and the counter.txt file to a webserver that supports PHP, you can see the script working.

When you push the “reload” or “refresh” button of your browser, you’ll notice that the number is increasing.

If it doesn’t work, this could have several causes:

-you made an typing error

-check if your webserver supports PHP

-it could be that you have to chmod the script and the counter.txt file ( on Unix/Linux servers).

The script itself should be worldexecutable ( 755) and the counter.txt file should be worldwritable (777).

I hope you learned something here and maybe it is of some use to you.

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. A Simple Hit Counter
  2. How to make your own PHP template script
  3. How to Make a Drop Down Template Theme Selector with PHP
  4. Using Sessions to Make a PHP Login Script
  5. Controlling Core Files (Linux)
  6. Tip: Create Multiple Files in XSLT 2.0
  • http://pulse.yahoo.com/_UONV7I26V4VLI6MN2BZTKZN5HA pratik

    thank you sir it was really helpful. i’ve a doubt, when i click the refresh button the count increments.. how can i stop that..? please mail the solution to my mail id: pratikgv1@gmail.com