I have a demo website for an e-test where each student is allowed 15 minutes for the whole exam (which comprises 5 subjects, each with 10 questions). I want it that, immediately a student clicks START on the home page, a countdown timer should start and update itself automatically. Actually, that is easily done for a single page.
The problem is if the student clicks to go the next page, I want the timer to begin from where it stopped on the previous page and continue counting down.
I thought of using SESSIONS but then how do I get Javascript to set the SESSION variable in PHP? The idea was to use AJAX but I do not know how to go about it.
Can anyone help me on it, even if it requires something other than AJAX?
PS:
What I want displayed is the number of seconds left, not the current time.
Thanks.
You don't have to know where it stopped on the other page.
You just want to know when the users clicked start.
Just save that in a database or a file even.
And on every request calculate the time left on the SERVER.
Since the client can be easily manipulated.
With the time left calculated on the server you can make a countdown using javascript on every page.
I found i simple way to do it, with a small search. Here
PHP:
//when you start
$_SESSION['start_time'] = time();
Then on every page:
<script type="text/javascript">
var startTime = <?php echo $_SESSION['start_time']; ?>;
//calculate remaining time
</script>
You can use an algorithm like this:
When the "start" event occurs, store the start time in a database.
Periodically send AJAX requests to a backend script. If the time has expired, return a response that ends the test.
There is not a real "timer" in this case -- just a start time, and logic to check if 15 minutes have passed since that time. If you want to display a timer to the user, the math will be straightforward.
You could just put the pages in IFrames and the countdown timer on the main page.
Simple, you store the timestamp of when the student first started the test and output it to each page. Then it's a simple matter of substracting current time from that timestamp to get elapsed time.
However, depending on Javascript is a very bad way of ensuring that this timer gets started. A better method is to force the user to click on an actual link/button, WITHOUT involving Javascript. You record the timestamp on the server then that hit comes in, and after that everything's fine.
start.php:
Start Test - Page 1
page1.php:
<?php
if(!isset($_SESSION['test_start_timestamp')) {
$_SESSION['test_start_timestamp'] = time();
}
?>
<h3>Question #1</h3>
etc...
The important thing is to never reset the timestamp value in the session once it's been set. Otherwise the test taker can simply click around until they reach a page where the counter's reset, and they can get more time for the test.
Related
Good day folks - be gentle - I'm a first time poster.
I have a page on a php-based web site that uses an AJAX-based call to update a DIV tag with a table of data every 5 or 10 seconds. The security folks where I work want me to make sure users get logged out after 15 minutes of inactivity.
To that end I put in
<script type="text/javascript">
window.setTimeout("location=('/mysite/session_timeout.php');",900000);
</script>
The problem is, of course, that the AJAX refresh of the table is 'resetting' the timeout counter ...
So my question is basically "Is there a client-side way to either mitigate the ajax call resetting the clock so to speak, or do I need to address this with php's _SESSION array and hide values in there, or does someone have something better than that?"
Thanks for any help you all can provide.
You can Store the start time of that page and update you table data without reloading the page. and check for the start time and destroy session accordingly.
You can try something like this
$_SESSION['activity'] = time();
function check_if_logged_in() {
if(time() - $_SESSION['activity'] > 900) {
// Do redirect or take other action here
}
}
Or you can use this jQuery plugin.
I ran into a little problem with my college project again :(
<input type="text" name="timrem" value="" />
This is a hidden field that I want the data from.
if(isset($_REQUEST['timrem']))
{
$tim=$_REQUEST['timrem'];
}
This is the code used to get the value from timrem.
The variable timrem is used to initialize a countdown sequence on the page(implemented using javascript)
<span id="countdown-1" style="float:right"><?php echo $tim;?>
The value of the hidden field is dynamically changed via java script counter implemented.
sremtime=(parseInt(minRemain*60)) + (parseInt(secsRemain));
document.frmTest.timrem.value=sremtime;
this works fine when the page is submitted, ie, the time doesn't restart from the next page. But when the page is refreshed, the timer is restarted to the value of the $tim when the timer first started.
Is it possible to somehow get the value of the hidden input on page refresh?
And also, please comment on the approach taken to preserve the time elapsed to the next page. This is a project on online examination.
If you need your variable to persist across refreshes, you cannot rely on information passed back from client to server. The only information you can rely on is the information passed with the very first request, and that obviously doesn't include your time. The best solution I can think of is to store your timestamp in a session variable on the server:
session_start();
if(empty($_SESSION['timrem'])) $_SESSION['timrem'] = time(); // set this if it is the first time this page is loaded
$tim = time() - $_SESSION['timrem']; // $tim is now the number of seconds since the page was first loaded in this session
You do not need to pass the time back and forth between client and server to keep track of it, although your javascript will probably need it.
A good idea might be storing a timestamp either in your PHP page or in a database (for multiple users), and use it to generate $tim. Passing data on refresh doesn't seem possible as there is no request triggered.
How about you store a "target time" (what ever time remaining is counting down to) in your url. you could even store it in epoch time:
myurl.com/stuff.html?target_time=1359553896
that way when the page is refreshed you still have it with you and you can calculate time remaining from that. if you don't want it in your url, you could probably hide it on the page somewhere with a display:none style.
how about something like this:
http://jsfiddle.net/QpZcV/1/
I'm a beginner.
I have a timer and progress bars for each item in a list. Bars progress sequentially. It runs whenever I load the page. I used cookies to make the timer stop from another page. But I want to keep the timer running even after closing the page.
I've thought of using a database to save the current time whenever someone accesses the page. So what I thought would happen is, the program will access the database whenever it opens the page then checks the saved time in the database and compare it to the current time to compute for the time elapsed and to be able to display how many progress bars should have finished progressing. But I don't know if this is a good idea. I bet not.
Is there any other way to implement this? Thank you. :D
Store the starttime or enddtime. Based on just that you can determine how much time is left.
At the moment I'm using this SQL to check if a user is connected:
date_add(last_activity, INTERVAL 3 MINUTE) > NOW()
This is passive and will only trigger when a page is loaded.
What I'd like to do is a system that checks if a user has remained connected during a given span of time and give them bonus points.
What do you suggest to achieve this? Use polling or another approach?
You need to use some kind of java script that will report activity using ajax to your site in intervals of like 1 min. You need to implement some sort of mechanism to check if user is active, like if the mouse moves or page is scrolled, because some users can use the same page for long times. If the page is not used since the last min(should be a global var in javascript, set to true every mouse move, and to false every time a message is sent) you don't send a message.
When you receive the ajax notifications in the server you check the time between the last ajax notification(should be stored in database) and current notification and if its smaller than an interval (maybe 2 mins) give the user points for it.
As said you can use AJAX and alternatively the Refresh META tag. Note that if I see a browser tab constantly refreshing, I close it as it is distracting. Of course, if you're offering a service that requires frequent refreshes (chat / shoutbox, ticker) then it's less disturbing.
I currently have 14 tabs open, some of which for days which I haven't really looked at, so you gotta ask how valuable your information is. As far as I know, javascript is unable to get the current mouse position if it's not moving over something that has the mousemove event handled, so implementing "is the page in view and the user moving her mouse" may prove to fire a lot of events when user is active.
I may be oversimplifying here, and this probably won't help much if you're trying to charge for time on the site or something like that, but this is something google analytics takes care of. Time on site is a metric they actively record.
Ok. So here is my idea. As I mentioned in comment my way would be javascript timeout and AJAX.
So the idea would be something like this:
Make a global variable for mouse position.
Set timeout for function to fire in around 10 second intervals (which can grow in time).
Function compares last mouse position with current mouse position. If it has changed user is presumably active.
Update global variable for mouse position. And send data to server-side.
Rinse and repeat...
Actually thing that changed can be anything if you want, but mouse position would be most suiting I think.
I've been looking around here on SO and Googling, however I can't find anything that fits my description.
What I want to do is update the database if the page has not been refreshed after 30 seconds. I want to email a person with the contents of a form {submitted by a different user} (I can do that) IF the person has NOT visited the page (I can do that) within the last 30 seconds.
What I've tried to do is make the page that should be visited refresh every 30 seconds, and so I figured if I did something like after 31 seconds, edit the database (so if the refreshed page was not refreshed, the database editing would run).
I'm sorry if this sounds complicated, there's probably a better way to do this, but I'm not sure how.
The bigger picture is I'm trying to make a 'on-duty' sort of thing, so that if the person is not actively looking at the page, they will get emailed with whatever the contents of the form is. The page will contain a table of all the entered form results.
You could update the database by creating a record with a timestamp every time the user refreshes the page. Then, you can have a PHP worker that looks regularely in the database if the timestamp is older than 30 seconds, and starts the e-mail.
I can't provide php solution for the server-side part of the job. But basically you need to set up javascript timer (eg. jquery timeout) and after 30seconds do the ajax call that will do what you want on the server (save something to db, send email and so on).
I hope I got your point and my advice will help you somehow.
maybe use an AJAX call every 30 secs based on the setTimeout javascript function?
http://www.w3schools.com/js/js_timing.asp
function timedCount()
{
# ajax call
t=setTimeout("timedCount()",30000);
}
I would suggest using javascript like this:
window.setTimeout("location.reload(true);", 3000);
This is of course if you desperately need to reload, but the user will obviously be mad that you reload the window for him.