NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page PHP: text/graphical hit/visitor counter in PHP

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-18-2004, 06:08 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



PHP: text/graphical hit/visitor counter in PHP


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/simpcount...impcounter.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 Code:
<?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
????: NamePros.com http://www.namepros.com/code/51954-php-text-graphical-hit-visitor-counter.html
    
$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)) ? 0;
    
$unique fopen($iplogfile"a");
    
fwrite($unique"$ip\r\n");
    
fclose($unique);
  
    if (
$curvisitor != 1) {
        
$file fopen($countfile"w");
????: NamePros.com http://www.namepros.com/showthread.php?t=51954
        
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], $i1);
    
    if (
$usetextcounter == 1) {
        echo 
"$curcount";
    } 
    else {
        echo 
"<img src=\"$imgdir$curcount$imgext\">\n";
    }
}

?>
Attached Files
File Type: zip simpcounter.zip (4.1 KB, 16 views)
deadserious is offline  
Old 10-18-2004, 02:04 PM   #2 (permalink)
Account Closed
 
axilant's Avatar
Join Date: May 2004
Location: /etc/passwd
Posts: 2,178
axilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to behold
 

Member of the Month
July 2005

i like the graphical one i can actually say i have never made a counter in all the 7 years of programming... lol
axilant is offline  
Old 10-18-2004, 04:48 PM   #3 (permalink)
NamePros Regular
Join Date: Aug 2004
Posts: 245
ichibaka is on a distinguished road
 



excellent, very small and easy to use.
ichibaka is offline  
Old 12-26-2004, 10:18 AM   #4 (permalink)
New Member
Join Date: Dec 2004
Posts: 2
Tresk is an unknown quantity at this point
 



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
Quote:
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:
????: NamePros.com http://www.namepros.com/showthread.php?t=51954

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 by Tresk; 12-26-2004 at 11:26 AM.
Tresk is offline  
Old 12-27-2004, 10:06 PM THREAD STARTER               #5 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



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.ph p"; ?>

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.
Last edited by deadserious; 12-27-2004 at 10:16 PM.
deadserious is offline  
Old 01-17-2005, 10:23 AM   #6 (permalink)
New Member
Join Date: Dec 2004
Posts: 2
Tresk is an unknown quantity at this point
 



it working now

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

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....
Tresk is offline  
Old 02-22-2005, 11:53 PM   #7 (permalink)
NamePros Regular
Join Date: Sep 2002
Location: Canada
Posts: 481
DarkDevil is an unknown quantity at this point
 



Good work deadserious, i like mine better

So.. didcha miss me?
__________________
Sometimes I lay awake at night and I ask "Where have I gone wrong?" Then a little voice says "This is going to take more than one night"
DarkDevil is offline  
Old 06-29-2005, 02:29 AM   #8 (permalink)
NamePros Member
Join Date: Oct 2003
Posts: 126
i386 is an unknown quantity at this point
 



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.
i386 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 08:53 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger