NameSilo

PHP: text/graphical hit/visitor counter in PHP

Spaceship Spaceship
Watch
Impact
6
This is a simple hit/visitor counter I threw together for anyone to use on their web site if they want. Feel free to modify it to suit your needs. You can download the entire script and installation instructions in the attached zip file. You can view a demo of it functioning as a text counter and counting only unique visits here http://www.coolesthost.com/simpcounter/unique/simpcounter.php and a demo of it functioning as graphical hit counter and counting all hits here http://www.coolesthost.com/simpcounter/simpcounter.php.

You can view the code below.

PHP:
<?php
  
    // CONFIGURATION SECTION - You CAN EDIT THE SETTINGS BELOW  //
    $countfile = "count.txt";	# The name/location of the count log file. You shouldn't need to change this.
    $iplogfile = "iplog.txt";	# The name/location of the ip log file. You shouldn't need to change this.
    $imgdir = "http://www.yourdomain.com/simpcounter/"; # The url to the directory contatining your images including the traling slash
    $imgext = ".gif";	 # The extension of your images, ( jpg, png, etc. )
    $usetextcounter = 0;	 # 1 = use text based hit counter, 0 = use graphical counter
    $countonlyunique = 0;	 # 0 = count all hits/pageloads, 1 = count only unique visits
    // END CONFIGURATION SECTION //

    $count = file($countfile);

if ($countonlyunique == 1) {
    $curtime = getdate();
    
    # Empties the ip log file if page is accessed between 12 and 12:01 AM #
    # Hopefully someone will access your page between 12 and 12:01 AM :D #
    if ($curtime['hours'] == 00 && $curtime['minutes'] == 00) {
        $file = fopen($iplogfile, "w");
        fwrite($file, "");
        fclose($file);
    }
  
    $ip = getenv("REMOTE_ADDR");
    $curips = file($iplogfile);
    
    foreach ($curips as $key => $value) {
        $curips[$key] = trim($value); 
    }
  
    $curvisitor = (in_array($ip, $curips)) ? 1 : 0;
    $unique = fopen($iplogfile, "a");
    fwrite($unique, "$ip\r\n");
    fclose($unique);
  
    if ($curvisitor != 1) {
        $file = fopen($countfile, "w");
        fwrite($file, $count[0]+1);
        fclose($file);
    }
} 
else {
    $file = fopen($countfile, "w");
    fwrite($file, $count[0]+1);
    fclose($file);
}

for($i = 0; $i < strlen($count[0]); $i++) {
    $curcount = substr($count[0], $i, 1);
	
    if ($usetextcounter == 1) {
        echo "$curcount";
    } 
    else {
        echo "<img src=\"$imgdir$curcount$imgext\">\n";
    }
}

?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
i like the graphical one i can actually say i have never made a counter in all the 7 years of programming... lol
 
0
•••
excellent, very small and easy to use.
 
0
•••
i dont know if this is the right place to post but i try...

i'm trying to use your simpcounter and follow the instalation notes
but when i comming to the step: "Call up the example.php file in your browser to get the code to add to your pages"

i only recieve this
Your system path to simpcounter.php is:

/simpcounter/simpcounter.php

Add the following code to all your pages where you want the counter to show up:

<?php include "/simpcounter/simpcounter.php"; ?>

With your current setting the ouptut will be:


Warning: main(/simpcounter/simpcounter.php): failed to open stream: No such file or directory in c:\inetpub\wwwroot\simpcounter\example.php on line 24

Warning: main(): Failed opening '/simpcounter/simpcounter.php' for inclusion (include_path='.;c:\php4\pear') in c:\inetpub\wwwroot\simpcounter\example.php on line 24


If you see a broken image above, then you need to change the $imgdir setting in the simpcounter.php file. If you have the settings in counter.php set to count all hits then try pressing refresh to see the counter increment.

You may visit the following thread to for more info and/or to ask any questions you may have:

http://coolesthost.com/showthread.php?p=64

and i have tryed a hole bunch of adresses in counter.php
and allso text based and graphical counter
but it still wont work
Do somone know what the problem is?
 
Last edited:
0
•••
It looks the script is not showing the full path in the example for some reason or another.

If you try it like this it should work:

<?php include "c:\inetpub\wwwroot\simpcounter\simpcounter.php"; ?>

Actually I just looked at the error again and it's saying the file is not found so I am not real sure.

Maybe try this one as well if the above don't work:

<?php include "c:\inetpub\wwwroot\simpcounter.php"; ?>

Or throw this in a php file in the same directory as the simpcounter.php file:

<?php include "simpcounter.php"; ?>


Maybe one of them will work. If not, then if you can show your phpinfo() for the server and provide a link to the script with the errors we could probably figure it out. :tu:
 
Last edited:
0
•••
it working now :bingo:

but im not sure what i did, to get it to work :red:

and i use this line now:
<?php include"./simpcounter/simpcounter.php";?>


but now i find that count.txt just showing 4 in it...all the time
but the counter on the page show 273 and it's more correct

allso iplog.txt is emty....all the time.

not a big problem really, but still anoying

anyway....thx for your time....
 
0
•••
Good work deadserious, i like mine better :P

So.. didcha miss me?
 
0
•••
If you're on a cPanel platform, instead of count.txt I would recommend using:
/home/{YOUR_USERNAME}/count.txt

Replace {YOUR_USERNAME} with your server user name. This won't allow anyone to see your counter unless they're on your main site.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back