unique visitors counter

Javascript Tutorials


Click on thumbnailed images to enlarge

Want to know how many unique visitors your site has? This script will count your visitors by keeping track of unique IPs. To see this script in action, [url=http://xquizit.cliquemb.com/php/counter/]click here[/url].

[size=8]The Code[/size]
First things first, open a blank page and save it as hits.txt. This file will record our visitors.

Now create a new file, save it as counter.php and paste the following code:

$filename = "hits.txt";

$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;

$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );

Don't worry about what all of this means. Basically, the code above records the number of unique visits (based on IP) to your hits.txt file.

Now upload both files to the same directory and ChMOD your hits.txt to 777.

To display the counter, you need to [url=http://www.createblog.com/forums/index.php?showtopic=82865]include[/url] counter.php wherever you'd like the number to be displayed:

<?php include("url/to/file/counter.php"); ?>

You'd probably want it to display as something like "XXX visitors since XX/XX/05":

<?php include("url/to/file/counter.php"); ?> visitors since XX/XX/05

Whichever file you decide to add this piece of code to must be saved as a .php file. Otherwise it won't recognize this snippet of PHP code.

Yep, it's that simple.


Copyright 2005 Xquizit
You are not authorized to copy or distribute any content from this tutorial.

Tutorial Comments

Showing latest 3 of 3 comments

god i hate PHP. i dont get any of it.
oh well i guess this is the best way to do it. just to clarify, hits.txt is saved to the web host right?

By butre on Jan 18, 2008 11:24 pm

This is awesome homeboy, thanks heaps.

By thechad on Jul 23, 2007 7:40 pm

Do you know if there's a way to make a counter without using PHP? My web host doesn't allow PHP files. If you can help me that'd be great. A link to my site can be found below. It's called Periscopic Dome.

By Indiscriminate Soul on May 22, 2006 2:46 pm

Tutorial Details

Author xquizit View profile
Submitted on Feb 11, 2006
Page views 26,314
Favorites 9
Comments 3

Tutorial Tags