|
|
|
A freeware Hit Counter by XdebugX. Source code is included!
Download Java Server Version
Download PHP Server Version
By XdebugX
I've made the hitcounter so that you can use php to update the hits instead of a java server application doing it. Now the applet only displays the hits, by reading the hits.dat file. A php script embedded in the same webpage as your applet tag, is now what updates the hits.dat file.
To display the counter add this applet tag to each page you want the counter on.
Here is a sample applet tag:
<applet codebase="http://yoursite.com/hitCounterFolder" code="hitCounter.class" width="168" height="30">
<param name=fileName value=hits.dat>
</applet>
The codebase tag tells the web browser where to look for the .class files of the hitcounter so that it can run the applet, the hits.dat file should also be in this folder. You can change it to the folder that you uploaded the hitCounter to on your webserver.
<param name=fileName value=hits.dat> - leave this as is, it just tells the counter what filename to use for the hits file.
For the next part we use inline php to update the hits.dat file, to enable inline php on an apache based webserver add the following to your .htaccess file: AddType application/x-httpd-php5 .html .htm .bla .wml
After you have added the applet tag to each page you want to display the counter on, you need to add some inline php to make the counter update:
<?php
$hits=1;
$myFile = "/home/yourSite/public_html/hitCounterFolder/hits.dat";
//getting file handle
$counter = fopen($myFile, 'a+');
//lock file
$lock = flock($counter, LOCK_EX);
//we are now in the critical section
rewind($counter);
// read hits
$hits=fread($counter, 32);
// goto start
rewind($counter);
// empty file
ftruncate($counter, 0);
// write new hit count
$hits=$hits+1;
fputs($counter, $hits);
//finally unlock
flock($counter, LOCK_UN);
//close
fclose($counter);
?>
You can put the php script before the applet tag, so it would add a hit before the applet started to display the number of hits.
You should change line 2:
$myFile = "/home/yourSite/public_html/hitCounterFolder/hits.dat";
Change it so that it points the the hits.dat file in the directory you put it on your webserver.
|
|
|
|
© Copyright xdebugx.net. All Rights Reserved. |
|
|