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

I have written a small simple script to log those who access my site and count the number of site hits. As of right now I have the script being called by an htmk image tag to create the logs but am having difficulty figuring out how to print back these results to the webpage when it loads.

Here is the script.

#!/usr/bin/perl $file = "/home/master/temp/perlskripts_ip.log"; $visited = "/home/master/temp/count.log"; $time = `date`; if ($ENV{'REMOTE_ADDR'} !~ /192.168.0.(\d+)/) { if (!-e $file) { `touch $file`; } open(lg, ">>$file") || die "Can't open $file!\n"; print lg "User from ".$ENV{'REMOTE_ADDR'}." logged on ".$time; close(lg); if (!-e $visited) { `touch $visited`; } open (vis, "<$visited") || die "Cannot open $visited for reading!\n +"; $count = <vis>; close (vis); print "count is ".$count,"\n"; open (vis, ">$visited") || die "Cannot open $visited for writing!\n +"; if ($count ne "") { $count++; }else{ $count = 1; } print vis $count; close (vis); }

and here is how I call it on my page

<img src="/cgi-bin/user_log.cgi" height="2" width="2" alt="[script]">
I really do not want to incorporate cgi unless I have to. Any ideas?

Replies are listed 'Best First'.
Re: Return script value in html?
by Roger (Parson) on Jan 23, 2004 at 00:23 UTC
    Yes this is quite possible. I do this all the time. ;-)

    <!-- example from my webpage --> <img src='/cgi-bin/show_bill.pl?cust=AGL&bill=2003-12-29&page=1' alt=' +2003-12-29'>

    1) your script must print appropriate Content-header - print "Content-type: image/jpeg\n\n"; or use CGI; my $cgi = new CGI; print $cgi->header( 'image/jpeg' ); 2) your script should generate an image with GD (or ImageMagick), or load a blank image from disk, and then annotate the image with hitcount calculated by your script. 3) print this image to the STDOUT, probably need to turn on binmode if you are on a windows platform.

Re: Return script value in html?
by b10m (Vicar) on Jan 23, 2004 at 00:03 UTC

    The problem is, once you ask for an image (<img ...>), your browser suspects to receive an image (so not text). So you'll have to create an image containing the lines you want to show to your visitors. This can be tricky, yet not impossible :)

    In this setup, you'll have to return an image, as explained above. You could create an image with Image::Magick. An other example for you (if you only want to return text) might be the use of Server Side Includes (SSI). The link will take you to an Apache doc, for I assume you use Apache.

    HTH

    I really do not want to incorporate cgi unless I have to.

    You already do! ;)

    --
    b10m