Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Simple Hit Counter

by Anonymous Monk
on May 29, 2006 at 01:59 UTC ( #552233=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info GreyFox
Description: This is a simple hit counter I threw together for my website. Nothing fancy, it just
#!/usr/bin/perl -w
#Simple hit counter

#To include in a webpage (HTML), put <!--#exec cmd="
+i"--> in the document
#Replacing PATHTOCGISCRIPT with the url to the CGI script (this script
#For PHP, within the <?php and ?> tags, include "virtual("PATHTOCGISCR
#without the quotes.  Also, you need to create
#a file named hits.txt for the script to access.  It can
#be blank

#Script copyright 2006 GreyFox of 


$file = "hits.txt";


open (COUNT,"<$file") or die "Counter error 1";
$hits = <COUNT>;
close COUNT;




print "Hits: $hits";


open (COUNT,">$file") or die "Counter error 2";
print COUNT $hits;
close COUNT;
Replies are listed 'Best First'.
Re: Simple Hit Counter
by gloryhack (Deacon) on May 29, 2006 at 20:55 UTC
    See perlopentut for a correct though still largely useless way to do this. The bit you want is in the "File Locking" section.
Re: Simple Hit Counter
by CountZero (Bishop) on May 29, 2006 at 19:37 UTC
    And of course the value of the counter is meaningless. The only thing it counts (if at all -- see the race condition/locking issue) is how many times the script ran and who would be interested in that?


    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Simple Hit Counter
by GreyFox (Initiate) on May 29, 2006 at 02:00 UTC
    Ok. I was logged in when I clicked on the link to post this. Oh well. I did make this.
      It's nice and simple, indeed. But (sorry ;) you will run into problems with it. As soon as two people access the counter at the same time, funny things will happen to your counter, because you don't use file locking.

      Have a look at perlfaq5 - web hit counter, or better yet, File::CounterFile.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://552233]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-09-30 00:47 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (155 votes). Check out past polls.