1

I would like to have a quiz with limited time for users to answer a question so they can't google. Unfortunately, this time protection can easily be bypassed if I do it with client sided languages like JavaScript. Is there a way to do this via PHP? It should be kind of secure.

Thanks in advance!

1
  • 1
    Save the time of last action in the session, then if time difference is > X print an error or have them start over
    – dualed
    Dec 21, 2012 at 17:42

2 Answers 2

3

Sure, simply tell the user they have the set time to do it, and set a session variable like so:

session_start();
$_SESSION['to_be_completed_by'] = time() + 60;

As time() gets the current epoch time in seconds, here you're giving the user a minute. And when they submit:

if($_SESSION['to_be_completed_by'] <= time()){
    // Completed within the limit
}else {
    // Not completed
}
1
  • Thanks you so much! I'm kind of new to PHP so thanks for helping me out :) Dec 21, 2012 at 17:46
2

$_SESSION['starttime'] = time(); on the render page, then $duration = time() - $_SESSION['starttime'] on the results page. Check if $duration > N (where N is the max duration).

This can also be coupled with javascript to alert the user their time is running low/out.

It should be noted that this would require session_start() to be called on the page(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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