We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Perl CGI Counter randomly skips

Stella Pauley
Medium Priority
Last Modified: 2013-12-25
I am using the code below to count hits on a web site.
Problem is it randomly skips on the tenth digit?
      For example 10 may show up but 20 shows as 10..:
This is random.. sometimes the tenth digit is correct sometimes it doesn't increment until the last digit hits 1.

This does not use FLY or SSI.. it is called with the following html:
          <img src="http://website/cgi-bin/counter.cgi?1">  
          <img src="http://website/cgi-bin/counter.cgi?0&w">

Here's the code:
  $digits_basedir = "http://website/cgi-bin/counter/";
  $count_file = "count";
($place, $Write) = split(/\&/,$ENV{QUERY_STRING});

open(COUNT,"$count_file") || die "Can't Read From Count File $count_file, Error : $!\n";
($count) = <COUNT>;
$count =~ s/\n//;

if ($Write =~ /w/i) {
   open(COUNT,">$count_file") || die "Can't Write To Count File $count_file, Error : $!\n";
   print COUNT "$count";
$count = "0000000000".$count;
@chiffres = split(//, $count);
@chiffres = reverse(@chiffres);
print "Location: ".$digits_basedir.$chiffres[$place].".gif\n\n";
Watch Question

I'm guessing the browser or your ISP is caching the image since the URL for the digits isn't changing.  Try adding a &x to the  <img src="http://website/cgi-bin/counter.cgi?1">   so it becomes  <img src="http://website/cgi-bin/counter.cgi?1&x">   that may get the cache into thinking it is dynamic (which it doesn't think it is currently).  The other thing to try is get to 19. close the browser, clear your cache and then go to the page and see if it pops up as 20.


I set it to &x and had the same problem.
I also ran it up to 119, closed the browser, opened browser, deleted cache files, then hit the page and got 110 instead of 120. then 121..

Thanks, any other ideas?
I may be barking up the wrong tree, but I still think it is a cache issue, probably the ISP or your network proxy server.
You might try putting the meta tags:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
<META HTTP-EQUIV="Expires" CONTENT="-1" />
on your web page and see if that makes any difference.


Same results with the meta tags.
Same results with or without going through a proxy.


You may be right on the caching though..
When the web shows 250 the count text file shows 260.
Try changing

print "Location: ".$digits_basedir.$chiffres[$place].".gif\n\n";


print "Location: ".$digits_basedir.$chiffres[$place].".gif?".$count.'\n\n";

oops - typo

print "Location: ".$digits_basedir.$chiffres[$place].".gif?".$count."\n\n";


same results...
Really!? When you right click (windows I hope) the image and look at the properties what does it show as the URL for the images? or look at the source.


properties of each image show:

images are layed out as:
4&x  3&x  2&x  1&x  0&w

Here's the "View Source" (yes its windows)
<META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
<META HTTP-EQUIV="Expires" CONTENT="-1" />
<img src="http://website/cgi-bin/counter.cgi?4&x">
<img src="http://website/cgi-bin/counter.cgi?3&x">
<img src="http://website/cgi-bin/counter.cgi?2&x">
<img src="http://website/cgi-bin/counter.cgi?1&x">
<img src="http://website/cgi-bin/counter.cgi?0&w">
Do you have the ability to change the links to https (page and the image links)? Just for the sake of testing?


Ability... Yes...
But I don't have a Certificate file for the ssl..

IIS v5.1
Unlock this solution and get a sample of our free trial.
(No credit card required)


IIS won't let me use ssl without a certificate file..

Thanks for the help..


I found the problem.. the &w was in the wrong spot..
I had it pulling image 4, 3, 2, 1, writing to the log file, then pulling image 0...

I moved the &w first image source (the one with a 4) instead of the on with the 0..
this way it writes to the log file then pulls image 4, 3, 2, 1, 0...

I still did need the cache disabled.. without that it then randomly skipped any digit..

Thanks for the help...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.