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

How can I use a Perl script to regulate the amount of time an HTML page is displayed (eg. a timed online quiz)? For example if I wanted a page with a quiz on it to dynamically load another page after 20 minutes.

Replies are listed 'Best First'.
(jeffa) Re: Timer for web page display
by jeffa (Bishop) on Jul 17, 2001 at 01:24 UTC
    Perl can be used to generate the meta tag that refreshes a page (try a Google Search for the syntax: meta refresh) or to generate JavaScript that does about the same thing. Perl is just a language, not a browser or a web server (although you could build both with Perl, those are wheels best left alone).

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Timer for web page display
by HyperZonk (Friar) on Jul 17, 2001 at 02:52 UTC
    Everybody is mentioning the "easy way out." Here's a sample for you. Suppose the next page that you want to display is www.mysite.com/testover.html. You want to switch to that page in 20 minutes (1200 seconds). You would simply include the following tag in the <head> section of your page (whether delivered from a Perl script or from a static document):
    <META HTTP-EQUIV="refresh" Content="1200; http://www.mysite.com/testov +er.html">

    Update: Hack noted by Cubes below is very true. Anything delivered in HTML to a browser can be saved and gone back to. An embedded app is probably the only way to do what you want without fear of compromise.
Re: Timer for web page display
by Cubes (Pilgrim) on Jul 17, 2001 at 02:59 UTC
    And, of course, your server side keeps track of when it spits out the form, using some unique token embedded in the form/session cookie/whatever, and when it receives the response for that uniquely identified form.

    Otherwise an unscrupulous test-taker could just save off the page, complete the quiz at their leisure, and then shove the form data back at your server, and you'd be none the wiser.

Re: Timer for web page display
by cLive ;-) (Prior) on Jul 17, 2001 at 02:25 UTC
    This is a little OT, but it all depends on what you want to happen next.

    Do you want to submit the quiz? Are you forcing users to have JavaScript enabled? If so, you can do something like the following (untested):

    # in head part of html output <SCRIPT LANGUAGE="JavaScript"> <!-- setTimeout(1200,'document.form_name.submit()'); // --> </SCRIPT> # and when defining the form, give it a name <FORM ACTION="wherever" NAME="form_name" method="post">

    That should submit the form for you after 20 minutes.

    If you just want to load a new page, look at META redirects.

    cLive ;-)

      Of course if I wanted to cheat, I'd just disable javascript and take my time on the quiz...

      I hate to say it, but you might be better off with a java applet. It'll be harder to f**k with (an enterprising user could play with the system clock...)

      UPDATE: mugwumpjism is correct that careful serverside validation should obviate the need for Java usage (good riddance). I don't know what I was thinking. I take comfort that my paranoia survived my muddy thinking. Trust not the client!


      TGI says moo

        Easily avoidable. Record somewhere the time that you issued the quiz to that session. On subimssion, check that it has been submitted within the allowable timespan.

        Or, encrypt the time that it was sent in a hidden field. When you get the form back decrypt the time.

        Remember to include somewhere the mention that attempts to circumvent the time limit will void the quiz entry.

        The key here is use three methods:

        1. Instruct the user to take less than 20 minutes on the test.
        2. Instruct the browser to automatically submit the form after 20 minutes using the javascript in above posts
        3. Enforce in the CGI script, by securely associating each issue of the quiz with a time, either stored locally or securely encrypted in the form.
        srand 3.14159; print join("", sort{rand 1<0.5}map{$_^"\037"}split m{ }x,"qmptk|z~wOzm??l]pUqx^k?j"),",\n";