View Full Version : hit counter tutorial?

11-06-2006, 06:06 PM
Hi, I've been planning to get into php for a while now but it's quite daunting after relatively simple languages like HTML and Javascript, so to finally make a start somewhere I got the idea to make a simple hit counter for my website.

I guess it's one of the most simple things to do with php... Though I could be totally wrong here.

I searched for some sort of a tutorial, but I only get premade hitcounters. If anybody could supply me with a link or somewhere to start I would be most gratefull.


11-06-2006, 07:19 PM
I can give you some hints to get started ....

1) Your page(s) will have the extension .php (instead of .html).

2) Where your counter appears, you have a PHP script that
reads in the counter value from a file (an integer that gets incremented
each time the page is viewed). That PHP script would either display that
integer or convert the digits to .gif images (for a graphical counter). Once
it displays the count, it increments the value and re-writes it back into
that file.

So, you'll have a file called: "count.dat" and this is the contents: 10236

Your webpage ("index.php") would be something like this:

<head><title>My Site</title></head>
This is my site.

$fp = fopen ("count.dat", 'r');
$count = fread ($fp, 1024);
fclose ($fp);

echo $count;

$fp = fopen ("count.dat", 'w');
fwrite($fp, "$count\n");
fclose ($fp);


NOTE: Make sure you set the file permission ("count.dat") to 777 so you
can write to the file ... to change the value.

11-06-2006, 08:18 PM
Thanks for the help. So, just to make sure I understand it all correctly, I should have:

1 - an external "count.dat" textfile with nothing in it but 10236? (why 10236?)

2 - an index.php file that is running the script?

3 - a php script somewhere in index.php?

Is it possible to make an external php script and then place a reference to it in all the pages I want it (counter in this case) to appear?

If yes, would this prevent me from having to rename all my .html pages to .php?

11-06-2006, 09:08 PM
your page here

<?php echo "$count"; ?>//place this where you want your counter

11-06-2006, 09:32 PM
I just picked the number arbitrarily.

Displaying counter on other pages? Do what Rafiki says ... but
it would be the counter from the main page. If you want unique
counters on each page, you would need to make each page have
an extension .php and a unique "counter.dat" file for each one.
"counter2.dat", "counter3.dat" ... etc.

Yes, so PHP "include" is easiest, but I thought you might want to
work with some PHP scripting to experiment with parsing the
integer and using some .gif images for numbers ... something you
can experiment with yourself.

Even with PHP "include", you still need to increment the number, so
you'll have to play with file read/write anyway.

If your HTML file has the extension .php, it is parsed by the server
as a PHP script. You'll see that the PHP script is put right inside
your HTML file (index.php). That's the whole powerful thing about PHP.
It can then be used to dynamically create your webpage. Not just a site
counter, but you can have it manipulate content, images, databases, etc.

Let your imagination go wild.

11-06-2006, 10:47 PM
Ah, that is what I thought. Thanks for elaborating.

Eventually I want to make a very basic highscore list for my javascript snake game. So yes, some experimentation is in order...

Thanks for the help so far, I'm going to try this out!

11-07-2006, 01:49 AM
Great, it's working. Suddenly doesn't seem as daunting as before, now it's just a matter of getting familliar with the commands. :)

However, count.dat doesn't actually increment with each visit, only when refreshing the page. I tried fooling around with it a bit, but that didn't work out...

Any idea what might be wrong?

11-07-2006, 02:18 AM
Ah, never mind, it was because the page loaded from cache instead of from the server...

11-07-2006, 04:05 AM
You could increase your sitemeter accuracy by using cookies
to only allow your current visitor one counter update no matter
how many times they hit refresh. But that's more advanced.

Learn basic PHP scripting by searching Google for many terrific
PHP tutorials available online. Viewing and playing with other
scripts is also a nice way to learn. Create a separate directory
in your webspace where you can play around with scripts and
not affect the rest of your site. Sort of like a "sandbox".

Have fun with it. It's addicting.

11-07-2006, 05:39 PM
thats where i started with php, there are more advanced tuts out there when your done with that

11-07-2006, 09:52 PM
Yes, w3schools is an excellent resource!

Thanks for the jump start guys, I'm diving in headfirst now. I'm sure I'll be back with some questions about a highscore table in the future.