Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Visitors Counter

by Anonymous Monk
on Jan 27, 2001 at 21:11 UTC ( [id://54757]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


I am making a counter for a website... first the counter was placed on the main page, then I was quickly faced with the fact that the count is erroneous because each time a visitor will go back to the main page, he will add to the counter.

So I made a splash page with the counter on it.. this fixed the actual visits.. but what if someone bookmarked the main page.. then his hits won't be recorded...

Question: What is the best way to make a visitors counter (not page hits counter) ??

  • is it by visitors IP ?? (not accurate though)
  • should I change the links to the main page from the other pages to ex: "../" or something like that??
  • ....

any suggestions?? is there something similar somewhere??

thanx in advance

Replies are listed 'Best First'.
Re: Visitors Counter
by Trimbach (Curate) on Jan 27, 2001 at 21:23 UTC
    The best (and most accurate) way to get to the information you want is to access the server logs directly. With log access you can track "user visits" as opposed to raw page hits by correlating visits with time and such. There are lots of freeware Perl log analyzers out there that will do this for you, but the trick is getting the log data in the first place.

    Unfortunately not everyone has access to the logs. If that's your situation you can try a technique I've used before with some success. On your main page (or any page you want to count visits) put a <img src=""> tag somewhere on the page. "", when called, adds a count to a file somewhere, and then sends back a 1 pixel invisible gif to the browser.

    The cool thing about this is that you're making use of a browser's cache behavior to prevent the same visitor from making multiple log hits. Most browsers will cache the output of and not re-call the script if someone hits the same page later. Cool, yes?

    Of course, it's not perfect as you are depending on the browser to prevent multiple counts (and believe me, it's never a good idea to depend on a browser to do anything :-P) but it should be more accurate than simply counting raw hits like you started with.

    Gary Blackburn
    Trained Killer

      Good suggestion, Trimbach.

      Just make sure to take into account multiple accesses to your file with the counter. Flock it, I guess, to prevent tramplin all over when 50 users open up your page. =)


Re: Visitors Counter
by tune (Curate) on Jan 28, 2001 at 00:32 UTC
    Trimbach's tips are almost complete. I would add the cookie-method. You send a cookie to every visitor's browser with a desired expiry date. 1 hour for example. If the browser does not send the cookie when visiting your page, you set it up and count a visit.

    That's simple :-)

    -- tune

Re: Visitors Counter
by mt2k (Hermit) on Jan 28, 2001 at 03:04 UTC
    The cookie idea is mostly good, but remember:
    • Some people actually don't allow cookies to be set
    • Some get *really* angry when useless cookies are thrown at them...
    • You might have some obsessive-compulsive people who just *have* to delete their cookies every two minutes :)
Re: Visitors Counter
by DiscoStu (Initiate) on Jan 29, 2001 at 20:37 UTC
    Randal has some interesting thoughts on counters man. You should check out . Very helpful.
    I am a mercy analyzes in crimson. I am a mercy analyzes in microns.
      I'm not certain, but I don't think merlyn likes to be referred to using his real name, unless you happen to know him.

      I remember seeing a message where he took someone to task for that. I can't find it now, but I'm sure it's in Super Search somewhere, if you're really curious.

      It would be also nice if you would take the extra moment to include the hyperlink code when pointing to outside sites.

        I'm not offended in the slightest by people using my realnametm.

        I do get a bit peeved when the second L sneaks in, so spell it right!

        -- Randal L. Schwartz, Perl hacker

        Forgive me for what I am about to write, brothers and sisters.

        This does strike me as a bit of a snotty response, IMHO.

        Lots of people use merlyn's real name on this site, and I
        bet he doesn't know all of em. (Most, probably, but never all :P).

        It seemed just the other day people were waxing lyrical on how to never turn away
        a valid contribution to the site, even if it is from a known Troll. Well,
        (compared to DiscoStu's track-record), this seems like a pretty valid contribution
        to the original thread. So what happens now, after he/she sees this reponse?

        Flame On, methinks.

        I am not advocating Trolling, but I felt I should comment on this node as it may turn out to cause more harm than good (I am NOT saying it was intended to cause harm, but ZydecoSue did seem quite terse and loaded with meaning.

        Ladies and Gentlemen, Boys and Girls, I give you, my two cents.

        Azatoth a.k.a Captain Whiplash

        Get YOUR PerlMonks Stagename here!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54757]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-16 20:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found