After 30 seconds, update MYSQL database - php

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.

Related

news ticker similar to facebook in php

I want to create a live news ticker similar to facebook in functionality.
I have already created a page where it will be having the news ticker ... Also, the database is ready to have the data ... What I don't know how to do is the rotation of the ticker as soon as new news have been added to the database ... I can do it when the page refreshes ... but I can't have it done live ... I wish somebody help me.
I have everything ready to be published, but waiting for the news-ticker ...
NOTE: I have only psudo-code no php finished yet.
You probably want to use timed AJAX calls. For example, every 1 minute send a request to the server to see if anything new is added. If yes, then display the new piece of news to replace the currently displayed one.
You'll need some kind of ajax-mechanism.
You can either poll a url every x seconds to see if there's any updates, you if you really want real-time, you'll need something like nodejs on the server (or anything that's not as cgi-like as PHP).
But if you want to keep things simple, I'd start with a simple Ajax solution and polling every x seconds.
I would suggest doing it with AJAX, poll the database once every 30 seconds for new entries.
AJAX let's you communciate asynchronously with the server, this way you can get new data without a page refresh.

Countdown variable across many pages

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.

Counting the time a user has been connected to a site

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.

Is this the best way to update user status?

Status as in online/offline..
When a user enters onto a page, it records the current time and puts it in the database.. if 5 minutes passes since their last action, it shows them as offline?
You can use JavaScript to time the five minutes, then use AJAX to call a PHP script that updates the database.
window.onLoad = function(){
setTimeout(UpdateDB,1000*60*5);
}
function UpdateDB(){
// AJAX call...
}
This would be a good way to start. You can easily check the period of inactivity.
It would be relatively simple for you to set up an AJAX ping to your server on a long interval~ so that as long as the user's browser has your site loaded, it's still pinging it's status as online.

php with javascript timer - what happen when user leaves the page before the timer is zero

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.

Categories