Skip to content Skip to sidebar Skip to footer

Php Timer Wait 30 Seconds Then Run A Command

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

Solution 1:

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.

Solution 2:

You could do this in Javascript using setTimeout();

var t=setTimeout("killPage",30*1000);
functionkillPage(){
    window.location='/fail.php';
}
functionquestionComplete(){
    t.clearTimeout();
}

Solution 3:

Try this:

php header('Refresh: 10'); 

It works for me maybe it will works for you.

Solution 4:

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

functiontimer($limit){
    if(!isset($_SESSION['start'])){
    $_SESSION['start']=time();
    }elseif(isset($_SESSION['start'])){
    $timeleft=time()-$_SESSION['start'];
    }
    if($timeleft>=$limit){
    $_SESSION['start']=time();
    }
return$timeleft;
}
echo timer(5); //Counts to 5 and then restarts

Solution 5:

$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////REDIRECTfunctiongetSecDifference($to,$from)
{

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

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


}

Post a Comment for "Php Timer Wait 30 Seconds Then Run A Command"