1

I'm just looking for a simple timer, where I can get my page to run a script after 30 seconds.

The idea is that the user has 30 seconds to submit an answer, otherwise the page will run a script and take them to a 'sorry, too slow' style page.

I cannot find the correct php function for this, but it basically we be like:

<?php

Start timer(30);

when timer runs out:
header("location: tooslow.php");
?>

Thanks for any help, Brett

1
  • 3
    You can't issue a header statement after sending html (body) content. Timer would have to be implemented in Javascript (but you can validate using PHP)
    – symcbean
    Jan 20, 2012 at 14:19

9 Answers 9

6

Store the page request time (from the time() function) in a session variable, then subtract the request time from the submission time (again, from the same function) when the user posts the answer. If the difference is larger than 30, they took too long.

Pseudocode:

<?php
session_start();
$_SESSION['question_start'] = time();
echo "Question: How long is a piece of string?";
...
?>

Then, have that page post to another, which does this:

<?php
session_start();
$time = time() - $_SESSION['question_start'];
if($time > 30)
{
    // fail!
}
...
?>

Of course, if you want the jump to the page to be automatic, you can use the above method in conjunction with one of the JavaScript methods in the other answers here. However, JavaScript alone provides absolutely zero guarantee that the user solved the question within the time limit, as you can just disable JavaScript.

My personal suggestion would be that you don't use a page redirect at all, but instead use Ajax to provide an interactive interface. Essentially, you use a setTimeout call as usual, but when it completes it does an Ajax request to a checking script written in PHP. That script would implement the session timing logic I provided above. If the time is OK, the script increases their score if they got it correct, then responds with the next question, which can be displayed on the page. If the time was too long, it sends back the next question but with a message that they took too long. This allows you to preserve the functionality of back/forward buttons in the browser.

2
  • 1
    Perfect, I have used this in conjunction with a timer on the page, so the user knows how long he/she has to submit their answer, many thanks for your and everyone else's help. Jan 20, 2012 at 14:45
  • I think the countdown timer is a great idea from a user interface point of view, as it not only informs the user of your intent to move them somewhere, but also provides extra tension!
    – Polynomial
    Jan 20, 2012 at 14:46
5

You could do this in Javascript using setTimeout();

var t=setTimeout("killPage",30*1000);
function killPage(){
    window.location='/fail.php';
}
function questionComplete(){
    t.clearTimeout();
}
3
  • This is disabled trivially, though, so it provides no actual guarantee of the time it takes. Just turn off JavaScript, adblock the script file, or write a Greasemonkey script to kill the timer.
    – Polynomial
    Jan 20, 2012 at 14:16
  • @Polynomial agreed, however the verification should be done on the server side, not the client side.
    – Paul Bain
    Jan 20, 2012 at 16:30
  • Yes, that's what I said in my answer.
    – Polynomial
    Jan 20, 2012 at 16:32
1

Try this:

php header('Refresh: 10'); 

It works for me maybe it will works for you.

1

You can try this function. It counts to your selected number and then restarts when reaches limit:

function timer($limit){
    if(!isset($_SESSION['start'])){
    $_SESSION['start']=time();
    }else if(isset($_SESSION['start'])){
    $timeleft=time()-$_SESSION['start'];
    }
    if($timeleft>=$limit){
    $_SESSION['start']=time();
    }
return $timeleft;
}
echo timer(5); //Counts to 5 and then restarts
1
 $timeLimit = 30; //seconds  

 $start = date("Y-m-d H:i:s",now());
 $end = date("Y-m-d H:i:s",now()+$timeLimit);

 for($x=0;$x<=$timeLimit;$x++){
    $next = date("Y-m-d H:i:s",now());
    $dif = getSecDifference($end,$next);            
    if($dif > 0)
        $x--;
    else{
        break;              
    }
}

///php will continue on to some action
///WRITE CODE HERE OR
////REDIRECT


function getSecDifference($to,$from)
{

    $date1 = new DateTime($to);
    $date2 = new DateTime($from);
    $interval = $date1->diff($date2);

    return number_format($interval->s,0,'','');


}
0

Although a timer can be used in PHP it's probably not the best solution here.

Your best bet would be to use Javascript setTimeout function (which runs a function when the time out has expired) on page load and then disable the time out if the submit button is pressed.

See http://www.w3schools.com/js/js_timing.asp

5
  • Since it's client-side, this can be broken trivially.
    – Polynomial
    Jan 20, 2012 at 14:11
  • Then in the result php page you should track the time the question is asked and the time the answer is recieved. if($answertime-$questiontime > 30) {/*Too slow*/} Jan 20, 2012 at 14:17
  • Yup. So you don't need the JavaScript at all. See my answer.
    – Polynomial
    Jan 20, 2012 at 14:17
  • Nah it is good to let the client side do stuff, like redirecting the user to the too slow page to let the user know his time is up. but never trust it! Jan 20, 2012 at 14:21
  • True, but automatic refreshes are nasty. I'd suggest that the checking be done via Ajax, so the user gets an interactive interface that doesn't interfere with the back/forward buttons.
    – Polynomial
    Jan 20, 2012 at 14:26
0

I think you'll want to do this in javascript, because if you use php sleep(30), the user wont be seeing anything until after 30 seconds.

The page will just keep loading.

What you can do is use the javascript function:

setTimeout('window.location = "tooslow.php"',30000);
0

Delay execution :

<?php sleep(30);
header("location: tooslow.php"); ?>

PHP.net

-2

You can add meta tag <meta http-equiv="refresh" content="30"> to the header of your page, so the page will be refreshed after 30 seconds without javascript. But you still need to have some php logic to deal with sessions or cookies so you know that it is the user whose time run out.

5
  • 1
    Please don't ever recommend meta refresh. It's bad for so many reasons.
    – Polynomial
    Jan 20, 2012 at 14:15
  • @Polynomial Anything that is on client-side is weak, i know, but php itself cannot refresh/redirect page after it was sent to client.
    – Sergiy T.
    Jan 20, 2012 at 14:20
  • This has nothing to do with client-side weaknesses. Meta refresh breaks the back button, has no cancellation mechanism, and sucks for anyone with a screen-reader. Besides, when W3C discourage something. it's usually best to heed their warning.
    – Polynomial
    Jan 20, 2012 at 14:24
  • Also the questioner doesn't want to refresh but he wants to redirect. Jan 20, 2012 at 14:25
  • @AndyPieters - You can redirect to any URL you like with a meta refresh. That's actually one of the reasons it's considered bad practice.
    – Polynomial
    Jan 20, 2012 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.