Zend - The PHP Company




Counters

Add Code


Website Hit Counter *Images or Text*  

Type: application
Added by: MaxDemian
Entered: 19/07/2002
Last modified: 07/12/2001
Rating: - (fewer than 3 votes)
Views: 7304
A script to count the hits to your website that checks if the current visitor was the LAST visitor to your website and if so doesnt count the hit and if not so counts the hit and makes the visitor the new last visitor. Also lets you choose if you want the number of hits to be printed out as text or printed out in gfx with the numbers.


<?
    
// Website Hit Counter *Image or Text*
    // Writer: Max Demian
    // E-Mail: maxdemian@hushmail.com
    // WebSite: http://www.destroymotorsports.com/
    // Language: PHP
    // Usage:
    // include("counter.php");
    // or
    // require "counter.php";

    // What you should know in advance:
    // The image files just need to be names 0.jpg/gif, 1.jpg/gif, etc. (easy)

    // Variables:
    
$style "img"// Style of counter "img" for an Image counter, "txt" for a Text counter
    
$cntfile "dat/hits.dat"// The filename where the number of hits is stored, if there isnt one then make one & put the number 0 in it
    
$lastfile "dat/lasthit.dat"// The filename where the IP of the last visitor is stored, if there isnt one then make one & leave it blank
    
$imgfolder "counterimages/"//The folder where your counter images are stored (must end in /)
    
$imgtype ".jpg"// Image type, like: .jpg .gif, etc.

    // None of this needs to be edited
    
$lf fopen ($lastfile"r"); // Open the file (for read) that has the IP of the last visitor in it
    
while (!feof($lf)) {
        
$whtlv fgets($lf4096); // Get the last IP
    
// End While
    
fclose ($lf); // Close the file that has the IP of the last visitor in it

    
$rs fopen ($cntfile"r"); // Open the file (for read) that has the number of hits in it
    
while (!feof($rs)) {
        
$cntr fgets($rs4096); // Get the number of hits
    
// End While
    
fclose ($rs); // Close the file that has the number of hits in it

    
if ($whtlv != getenv("REMOTE_ADDR")) { // If the IP of the last visitor is not the same as the IP of the current visitor
        
$wr fopen ($cntfile"w"); // Open the file (for write) that has the number of hits in it
        
$cntr++; // Increase the number of hits by 1
        
fputs($wr$cntr); // Insert the new number of hits into the file
        
fclose($wr); // Close the file that has the number of hits in it
        
$nl fopen ($lastfile"w"); // Open the file (for write) that has the IP of the last visitor in it
        
fputs($nlgetenv("REMOTE_ADDR")); // Insert the new IP into the file
        
fclose($nl); // Close the file that has the IP of the last visitor in it
    
// End If

    
if ($style == "img") { // If you chose your style as an Image counter
        
for ($i 0$i strlen($cntr) + 0$i++) { // for each number in the number of hits
            
echo "<img src="" . $imgfolder . $cntr[$i] . $imgtype . "">"// print <img src="IMAGEFOLDER/IMAGENUMBER.IMAGETYPE">
        
}
    } elseif (
$style == "txt") { // If you chose your style as an Text counter
        
echo $cntr// Print the number of hits
    
// End If
?>


Usage Example




Rate This Script





Search



This Category All Categories