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

Hi All! I've been having a problem with my counter script. I dont want to use SSI so i decided to use the 'IMG src=myscript.pl' method ;)
My problem is, if I want to print 5 digits (00126), can I just say:
print "Content-Type: image/gif\n\n";
print_binary_data(0);
print "Content-Type: image/gif\n\n";
print_binary_data(0);
print "Content-Type: image/gif\n\n";
print_binary_data(1);
print "Content-Type: image/gif\n\n";
print_binary_data(2);
print "Content-Type: image/gif\n\n";
print_binary_data(6); can I use multiple 'Content-type's ? If I Can't ? how can I return my five digits without having to use external modules (ie: GD.pm) How can I concatenate my 5 digits file ? help...please ... HELP ;) thanks J-D

Replies are listed 'Best First'.
Re: Counter problem
by trantor (Chaplain) on Sep 01, 2001 at 00:08 UTC

    Is it always the same number of digits? E.g. 6?

    Your HTML could look something like:

    <IMG SRC="digit.pl?position=5" ><IMG SRC="digit.pl?position=4" ><IMG SRC="digit.pl?position=3" ><IMG SRC="digit.pl?position=2" ><IMG SRC="digit.pl?position=1" ><IMG SRC="digit.pl?position=0"

    The > is at the beginning of the next line to prevent some browsers (e.g. Netscape) to insert whitespace between one digit and another.

    Now all you have to do is:

    • have 10 image files, one for each digit;
    • find a way to differentiate between sessions, so that the counter progresses properly.

    For example if you dynamically generate the page, you could have something like:

    <IMG SRC="digit.pl?position=5&session=foobar-id" ><IMG SRC="digit.pl?position=4&session=foobar-id" ><IMG SRC="digit.pl?position=3&session=foobar-id" ><IMG SRC="digit.pl?position=2&session=foobar-id" ><IMG SRC="digit.pl?position=1&session=foobar-id" ><IMG SRC="digit.pl?position=0&session=foobar-id"
    or even (which is text browser friendly):
    <IMG SRC="digit.pl?digit=0" ALT=0 ><IMG SRC="digit.pl?digit=0" ALT=0 ><IMG SRC="digit.pl?digit=1" ALT=1 ><IMG SRC="digit.pl?digit=9" ALT=9 ><IMG SRC="digit.pl?digit=7" ALT=7 ><IMG SRC="digit.pl?digit=3" ALT=3

    Other possibilities include cookies.

    This method makes it easy to glue together many images in your HTML, if it is dynamically generated, for example the result of a CGI. Otherwise digit.pl has to go through a god deal of work (presumably) to generate an progressive number for each visitor, which does not mean increase the counter every time digit.pl is called, not even every six times digit.pl is called, because basically you have no idea of what could happen.

    So you may want to consider this solution if you dynamically generate your HTML, otherwise you require much more effort to make digit.pl work reliably for each digit and for each session/user.

    -- TMTOWTDI

      six cgi calls seems a little over the top for a counter. Any tag which results in a call back to the server will do. Iframes and layers are a bit picky, but how about:

      <script language="javascript" src="/cgi-bin/counter.pl"></script>

      and have the perl script check the referer and return a simple javascript? You could use it to change the .src of six images that already existed, or perhaps just document.write('123456'); would suffice.

      nb. remember it would be:

      print "Content-type:application/x-javascript\n\n";

      and not the usual text/html.

Re: Counter problem (boo)
by boo_radley (Parson) on Aug 31, 2001 at 23:22 UTC
    Content-Type is a header, you get one per document, and I don't think that's what you want.

    Depending on what print_binary_data does, you could just replace it with a series of 10 images, each showing a single digit, and then reading your counter file, as below.

    use CGI qw(:standard); my $lines=<DATA>; chomp $lines; while ($lines=~/(\d)/g) { print img{src=>"$1.gif"}; print "\n"; } __DATA__ 5123496780
Re: Counter problem
by blakem (Monsignor) on Sep 01, 2001 at 01:03 UTC
    If you're on a unix machine and don't want to use perl modules, you should check to see if the 'montage' program is installed. (type 'man montage' or 'which montage') It can merge several individual digit images into a single one. Here is a line of code from an early program of mine that used montage in this way...
    $image = `/usr/X11R6/bin/montage +display -borderwidth 0x0 +label +fra +me -geom etry ${scale}+0+0 -tile ${tiles}x1 $gifs GIF:-`;
    It won't work right out of the box for you, since you'll need the digit images, and then some code to put their filenames into the $gif tag.

    Hope this helps.

    -Blake