Reputation: 17
I want to build an online quiz test site.
Suppose, exam will start at 10:00 am and a student login to give exam at 9:45 am. Whenever the student clicks 'Take Exam' button, he/she cannot not get access to the question paper because the exam time is 10:00 am and there are still 15 minutes before the exam start.
Now I want to put some Javascript or PHP code that will prohibit the students to give exam earlier and if a students come early, it will show a stopwatch which display the remaining time before exam time and when the current time is equal to exam time then he/she will be directly redirected to the question paper page.
Upvotes: 1
Views: 301
Reputation: 2434
At the top of every PHP script you want to disallow early access to, you would want to include something like:
if (mktime(12, 0, 0, 1, 1, 2012) > time()) {
require 'waiting.php';
die;
}
You should check the mktime() PHP documentation to learn how to specify the date and time you wish you use as the threshold.
The above example would include the waiting.php
script when the current time is less than Midday, on the 1st of Jan, 2011). The die
function prevents the PHP from loading anything else in the page, effectively preventing access. When the current time exceeds your mktime()
time, the include
and die
calls will never be trigger, and the page will be loaded as per normal.
Be wary of the system time. mktime()
will use the system's time, which may be a different timezone than the one in which you want to run your test. You should use date_default_timezone_set() to set your timezone before calling mktime()
.
For example:
date_default_timezone_set('America/Los_Angeles');
Lastly. as mentioned in other comments, JavaScript detection alone is not reliable, not only because it can be easily circumvented, but also because there is no guarantee that the user's time is the same as the server's.
Say you wanted to refresh the page once the timeout is complete. I would include in waiting.php
the following:
<?php
$miliseconds_remaining = (mktime(12, 0, 0, 1, 1, 2011) - time()) * 1000;
?>
<script type="text/javascript">
window.setTimeout(function() {
window.reload(true);
}, <?php echo $miliseconds_remaining;?>);
</script>
This will force the page to automatically refresh, and ignore the browser's cache, when the time expires. This solution will refresh the page irregardless of timezone differences between the server and the user.
Upvotes: 0
Reputation: 40185
Anythign client-side is hackable. Stick to pure server side.
Why not a very simple PHP page which, when loaded, checks the time and redirects appropriately?
test.php
if ( now() < $test_start_time)
{
header('Location: come_back_later.php');
Die;
}
// test page goes here
Upvotes: 2
Reputation: 63962
I wouldn't trust JavaScript for this since the user can disable Javascript on his/her browser to bypass the security. You should look at some server-side approach, perhaps some script that would publish your website at the specific given date and time.
You did not specify your OS and web server, but whether you use Linux/Apache or Windows/IIS, doing something like this from a batch script shouldn't be too difficult.
UPDATE
Have 2 landing pages for your app, one with a body saying something along the lines of this app will be available starting at 10 AM
and create a Task (Control Panel-> Admin Tools -> Task Scheduler
) that would replace this temporary landing page with the real one at 10 AM of the given date. Is that easy enough?
Upvotes: 0