Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I've created a hit counter for a friend.

How it works is, they put an image tag into their page, WIDTH=1, HEIGHT=1, and SRC=http://myserver.com/counter.pl

From that, every time they get a hit, I get a hit too and can count it and do whatever else needs doing.

What I want to know is, what should I return from that hit so as not to generate errors every time it happens?

I'm assuming I can use a Content-type: image/gif or similar to send back something, that way there won't be an error generated, but what do I send back? What's the minimum code I can send back which will "convince" the browsers/servers involved that it is a gif when in fact it isn't?
--

($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
Re: Hit Counter Using IMG Tag To Call Script
by tstock (Curate) on May 12, 2002 at 06:06 UTC
    You could just redirect to a real image, or read a image file and print that our after content-type: image/png (or whatever).

    Image hit counters are not really great though, mining http log files are better when available. Remember to consider concurrent requests and file locking if you write to a file instead of a database, and remember you are counting requests, not individuals. Consider file caching by the browser or proxies (some http headers may help in this case).

    tstock
Re: Hit Counter Using IMG Tag To Call Script
by belg4mit (Prior) on May 12, 2002 at 06:23 UTC
Re: Hit Counter Using IMG Tag To Call Script
by Chady (Priest) on May 12, 2002 at 07:01 UTC

    this is a single pixel transparent gif:

    print "Content-type: image/gif\n\n"; print pack "B*", '0100011101001001010001100011100000111001011000010000 +000100000000000000010000000010000000000000000000000011111111111111111 +111111100000000000000000000000000100001111110010000010000000001000000 +000000000000000000000000000010110000000000000000000000000000000000000 +000010000000000000001000000000000000000000010000000100100010000000001 +0000000000111011';

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

      Same gif, 1/4 the length:

      my $gif = pack "H*",'47494638396101000100800000ffffff00000021f90401000 +000002c00000000010001000002024401003b'; binmode STDOUT; # for Win32 users print "Content-type: image/gif\r\n"; print "Content-length: 43\r\n\r\n"; print $gif;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        ah, gifgolf
        Ooo, can I try? Here's a smaller one:
        use MIME::Base64; my $gif = decode_base64('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABA +AEAAAICRAEAOw=='); binmode STDOUT; # for Win32 users print "Content-type: image/gif\r\n"; print "Content-length: 43\r\n\r\n"; print $gif;

        -sam

Re: Hit Counter Using IMG Tag To Call Script
by Anonymous Monk on May 12, 2002 at 17:27 UTC
    Sending a 1 pixel transparent gif would actually only take up about 40 bytes (if you have a good utility for making it, some could take up to 1K). Also, you could return a JavaScript that just does a document.write(''); sort of thing.