How to SET TIME LIMIT For user to view page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xenomaniacz
Forum Newbie
Posts: 2
Joined: Sat May 10, 2008 3:58 pm

How to SET TIME LIMIT For user to view page?

Post by xenomaniacz »

Hi,

I'm currently working on a php+mysql web based project. The website is something like an online tuition center. So, I need to add a section for user to be able to do exercises or examinations online.

My problem is, I need a countdown timer or something like a time limit that limits the user to do the exercises/exams within 60minutes or 120 minutes (Prefer if time limit can be set).

Can you guys point me out which website that offers a code that would work like that? Or a sample code i can refer to, so that i can put this function in my website?

Thanks :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to SET TIME LIMIT For user to view page?

Post by onion2k »

Why don't you write it yourself?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: How to SET TIME LIMIT For user to view page?

Post by nowaydown1 »

I imagine you're probably shoving those test results into a database. So why not add a column on one of your tables that gets updated when the exam is started by the person. Every time they answer a question, you could reference that value you inserted into your database to see if it's been more than 60 minutes or 120 minutes or whatever the duration of the exam is. If that time has passed, then you can handle it however.

For a user facing component, I'm sure you could find some javascript countdown timer or something along those lines.
xenomaniacz
Forum Newbie
Posts: 2
Joined: Sat May 10, 2008 3:58 pm

Re: How to SET TIME LIMIT For user to view page?

Post by xenomaniacz »

errmm...I need it to work like this...

1. User clicks "Start Exam"
2. Exam page loads. (Upper corner shows time left until exam finishes.)
3. After 60 minutes, automatically load "End of Exam" page.
4. If user did not finish answering all questions within the given 60 minutes, those questions unanswered will not be given marks.
etc....

P/s: onion2k, if ppl know how to write it, this forum is basically useless...

Sorry but I'm a newbie in php, i only done 2 projects on php so far. There r alot of functions and stuff in php that i dont know. :banghead:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to SET TIME LIMIT For user to view page?

Post by onion2k »

xenomaniacz wrote:P/s: onion2k, if ppl know how to write it, this forum is basically useless...
This forum is here to help you learn. We'll happily lend a hand when you get stuck. We aren't here to point you to ready made solutions that you can just drop into your code though, which is what you're asking for. nowaydown1's response was great ... he told you how the code should work (albeit from a server side point of view rather than clientside as you seem to want). Now it's your job as a coder to think about that, learn new PHP and Javascript functions, and write the code. You can't be expecting to get some working code without putting any thought into it yourself, surely?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to SET TIME LIMIT For user to view page?

Post by Chris Corbyn »

On the server the simplest implementation would be to store the value of time() in a session variable when the page is loaded:

Code: Select all

if (!isset($_SESSION['start_time'])) { //Don't overwrite it
  $_SESSION['start_time'] = time();
}
Then refuse submission of the results if more than 30 minutes has passed:

Code: Select all

if ((time() - $_SESSION['start_time']) > (30 * 60)) {
  //User cannot submit test - 30 mins already passed
}
You'll always have to have that server-side logic there since it'd be too easy to evade from the client side. Even using the session is easy to evade though. Without forcing the test to only be available at a fixed time period (UTC) then there'll always be a way around it.

To have the timer you need JavaScript, it's Date object and a function which runs every 1000ms (window.setInterval(...)). Each 1000ms grab the current time (should be 1 second passed, but just double-check anyway) then calculate the number of seconds remaining and write to a <span> or <div> somewhere on the page with a known ID.

The JavaScript but is going to be the hardest so maybe if you go away and do the PHP part we can come back to look at the JavaScript more closely.
Post Reply