Simple Text Based Hit Counter For Static HTML Pages

by Hiroshi on June 13, 2008

in Hit Counter II

We can create a quick simple text based hit counter for any web page or website and there is no database requirement for that. Then we can include it in a PHP file and call it via iframe in any html webpage.

You will be needing two files for that.

1- one blank text file
2- PHP code file

Remember when you will upload the text file, make sure its properties are set to write (CHMOD must be set to 777. If you do it through your favorite FTP software then just right click at file and click properties/CHMOD and write in permissions box 777 or check all boxes of owner, group and public. Because we have to write in that file and file will be updated so we need write permissions at that text file).

Usage (Code)

if (file_exists('count_file.txt'))
$fil = fopen('count_file.txt', r);
$dat = fread($fil, filesize('count_file.txt'));
echo $dat+1;
$fil = fopen('count_file.txt', w);
fwrite($fil, $dat+1);
$fil = fopen('count_file.txt', w);
fwrite($fil, 1);
echo '1';

While text file (count_file.txt) will just contain any number i.e. 1 or 200 or as you like from where your count must start. Put above script in your php file where you want the text counter to appear and upload a text file with some number in it i.e. 1, in the same directory.

If your pages are static then create a php file and include the php above code file in that. After making it call this file in static home page by iframe. In this method you will be needing a text file as i mentioned above, php code file which is given above and finally this php file inwhich you will include or call that php code file. Finally call it through iframe anywhere in the website or HTML page.

Include Code For file counter.php

include "count.php";

*While count.php is the file containing all counter code above.

<iframe name="some-name" src="counter.php"></iframe>

Include function in php has same functionality as iframe in static page but include is more powerful because it actually writes the code in the output page rather than placing another page with scrollbars in the main page as it is in iframe behavior.

Download Counter With Include method

Related Posts

Previous post:

Next post: