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

I have a perl script that works fine but I would like to know how many times that script has been run on the web.
All counters I have found will tell me how many times a link is clicked but not how many times the submit button on a form has been used. Mayby inside the script a simple ( open data file and add one ) would be suffice .
Any suggestions.

update (broquaint): title change (was Count.) + added formatting

  • Comment on Count how many times a CGI script has been called

Replies are listed 'Best First'.
Re: Count how many times a CGI script has been called
by little (Curate) on May 04, 2003 at 11:03 UTC

    nasa,

    When your script is called via a "submit" button then your server will get a document request for your script using request method POST. This will look alike:

    217.9.44.215 - - [04/May/2003:12:47:35 +0200] "POST /cgi-bin/nasa.cgi HTTP/1.1" 302 5

    In this way when determining your web server access logs you would see all SUCCESSFULL calls to that script and in your error scripts then those calls to the script that caused an internal server error, which happens if your script dies or will bot be executed due to an error.

    So I would go with Abigail-II to use the server logs for that counting task at its already there, you just need to grab those infos from them.

    try to search for "web log anlalyzer" here at perlmonks or at cpan or sourceforge or even google and you'll find plenty of tools who already have been invented and mostly heavily tested, which enable you with small modifications or just custom initial settings to fullfill your task.

    I would not recommend appending to a file as that consumes your web servers ressources and is already done in other places by the web server itself.

    Have a nice day
    All decision is left to your taste

Re: Count how many times a CGI script has been called
by Abigail-II (Bishop) on May 04, 2003 at 09:52 UTC
    What is the difference between clicking a link, and clicking the submit button, apart from some webbrowsers rendering it a bit differently? On the serverside, it doesn't matter. If you can make a counter for "clicking a link", you can make a counter for "clicking a submit button". There's no need to modify a single bit of the code.

    Abigail

Re: Count how many times a CGI script has been called
by CountZero (Bishop) on May 05, 2003 at 19:19 UTC

    Alaso remember that your CGI script will be called many times, perhaps even (almost) simultaneously and that if you go for the "open data-file and add one to a value in this file", you are opening yourself to various race-conditions (such as: script instance A opens datafile and reads value; meanwhile instance B of the same script does likewise; instance A increments value and writes it back to the file; instance B does the same. Result: only 1 is added to the counter and not 2.)

    And we are not even considering the bad things which can happen when your data-file gets opened simultaneously multiple times for reading and writing. This could confuse the best of Operating Systems!

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law