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
You could do this in Javascript using setTimeout();
var t=setTimeout("killPage",30*1000);
function killPage(){
window.location='/fail.php';
}
function questionComplete(){
t.clearTimeout();
}
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.
Try this:
php header('Refresh: 10');
It works for me maybe it will works for you.
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
$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,'','');
}
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
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);
Delay execution :
<?php sleep(30);
header("location: tooslow.php"); ?>
PHP.net
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.