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.
Related
I am implementing an online exam portal, so that a user can start the mockup test(exam) and choose the anwsers for each question and proceed to the next question.
Rules for the exam is to give 100question to complete in 75mins.
So I need my back-end code to check each bit of time and track if the current_time not exceed 75min from the Exam_Start_time
How is this possible.
I made it like this for time being
$Start_time
$Current_time
and then check the difference on each page refresh and redirect if 75min limit exceed
But I think its not the better way and if we can trace it dynamically and redirect when the 75min mark reaches to the process the exam result it would be great.
Can any one help me in this context,
Is there a way if its not possible with PHP, HTML to use Javascript to achieve this
Hope to hear from you stacker.....thanks in advances
Store in your database time and some unique id for each user. While user take a test send ajax request with some interval (i.e per minute) to the server with user unique id and check is everything ok with time if not redirect him to another page or block old one with javascript. But think about security, some user can guess and send another's id :)
If user disable javascript there is another scenario. Server closes tests which hasn't been updated for some interval. And also about local time and javascript you haven't to send user's time to server because you have start time in database.
#trejder and #Wiz if think as you do it's better to do not use javascript at all as it can be turned off and request variables can be falsified by user.
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.
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 have a game whene the user can win something.
as soon as he wins he has 60 seconds to fill in his adress name etcotherwise he lose his price and someone else can take it.
when he not fills in the adress in 60 seconds then he gets redirected to a sorry too late page and he lose his price.
when he fills in his adress he gets redirected to a confirmation page and can claim his price.
i made this with php and the counter is in javascript.
the javascript will do the redirect when time = 0
how can i controll that the user either goes to the confirmation page or to the soory to late page.
i'm wondering how i can be sure that the javascript countdown is really counted to zero even wehn the users leaves the page.
thanks a lot
You can't be sure - your users can easily modify your javascript to increase the time limit or even disable the timer completely.
The time limit should be validated on the server. I would suggest that when the time limit starts the server stores a row in the database storing which user it is and when the time limit started. When the user submits the server should check the database to see how much time has passed.
I also think that having just 60 seconds to fill in your address is rather harsh, but that's your decision.
try not to trust only javascript for this job, use php to write the time in the session or an database when the user gets the form to fill in his data - check the difference (with php) between this starting-time and the end-time when the user submits the form to the server
and show him the confirmation when diff<=1min or show him sorry-to-late when diff>1min
you could do this, use javascript for user interfase, and when user leaves a filed execute ajax call to php script and save data that he just entered in field. And real counting make in php, so when user submits final data u should also check if time expired in php.
I think this is good solution for your problem.
If you do this in Javascript, anybody can read your script and find where the prize page is.
Instead, run your timer with PHP using microtime(). You can start when they see the "You Win" page. It will run in the background, and won't depend on any user interaction.
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.