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.
Related
I have a game site, in which i need to display timer until the match time. Once the timer reaches the limit, i need to load the games.
For this, i have used the jquery countdown. Its working fine in general.
My problem is, while the timer running if the user changes their system time(local time), the remaining time limit changed and game started.
How to prevent the Countdown Timer from local time? Please help me to fix the problem.
My code,
$('#gamewindow').countdown({
until : 100,
layout:'{mn} : {s<}{snn} {s>}',
onExpiry: function () {
$('#gamewindow').load('game.php');
},
});
});
<div id="gamewindow"></div>
Instead of initializing timer via plain Javascript (which will return the client-time, i.e., the time on your client's computer), you should instead rely on server-time, i.e., the time on your server. This will standardize the timing for all your players.
So, upon visiting the countdown page, your Javascript counter will be initialized with the server's time. You could use AJAX or some hack-y PHP to do this. For this task, start-up is the only time JS needs something from the server; afterwards, the "countdown" effect will be managed by JS. Of course, this will not be uber-synced but that error is pretty forgivable. Even if the user refreshes with a different local timezone setting, the JS will just re-initialize from server time--no problems!
Since you can't prevent the user from changing their system time, the solution must be serverside.
In game.php, check if the match_start_time has been reached, if not, redirect to the waiting page (which should display the countdown timer again).
I have a user status field (online/offline) in database. when user hit logout button at that time in database change online to offline. but if user directly close browser without hitting logout then how to update database.
Unless you have a process which pings your servers at a regular interval, you can't.
Your logout button most likely sends a request back to your server. Upon receiving that request, the server runs/delegates logic to update the DB. If the user closes the browser without clicking the logout button, the server never gets this request.
#Antony gave a possible solution.
Another possible solution would be to send a message to your server at a given interval. The server should expect this call. If the server doesn't receive the message, then mark the user as being logged out. It has the downside of the logout timestamp being off by the interval. The logout time will not be exact. See this thread for more detail Ajax call with timer
Edit #1
In the link I mentioned above it mentioned how you can run javascript code at an interval.
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
In your case use this function to call your API.
Your API should record timestamps of when it last received a call from your javascript. Have a cron job, or another piece of logic to check when the recording for that particular user stops. The last timestamp would be the approximate time the user logged out.
It's a very involved process for simply tracking a user's logout behavior. You may want to consider if it's worth the trouble.
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.
I have a simple chat client set up that allows users to login with a username and stores the messages they write in an sql database. Every 3 seconds, the database simply prints of all the rows. So it's basically a chat client.
I'd like to keep a list of who's online. How can I do this? How can I sense when someone has closed the browser?
Right now I'm pulling the username as
$name = $_COOKIE["name"];
and if this value is empty, I know they left. But once they left, it's too late to know what their username was so I can't keep track of who exactly left.
Ideas? I'm fairly new to php, javascript, and html, so keep that in mind :)
It is hard to send a last request to the server when someone closes the window, since Browsers usually don't wait for JS execution to finish when the user wants the window closed (as is the case with onbeforeunload).
Whenever I am confronted with a situation like this, I tend to use onbeforeunload to send a final request (which happens fast and usually finishes before browser window is closed), but also implement a timeout feature. The timeout feature would work as follows:
Everytime the user sends something to the server, the server recognizes this as 'still there'. At the same time, the client sets a timer of, say, 45 seconds. If the user does not type anything for 45 seconds, the client sends a 'still alive' signal on its own to stay connected.
Now, the server should execute a removeInactive() routine every 60 seconds (allow 15 seconds slow-connection margin, hence the 45/60 seconds) which removes any users who haven't sent a 'still alive' signal in the last 60 seconds.
This system worked fine for me so far, you could try it out for yourself.
Assuming your using AJAX to pull in the chat messages every X seconds, update your table of users at the time with the current timestamp for that user. Then anyone who has a timestamp say older than 10 seconds, you will know they have left the page.
put online users in a table that will have a field named like 'lastSeen' update this field every few seconds with ajax call..
ajax call be made someting like this :
window.setInterval(function() {
$.ajax({
url: _URL_ENGINE + "/updateLastSeen/?userid=" + userID,
success: function(data) {
}
});
}, 2000); // 2000 means 2 seconds
now to query the list of online players u can query them like
select * from players WHERE lastSeen > DATE_SUB(NOW(), interval 40 SECOND)
hope this helps
Use the onbeforeunload event.
You could possibly attach to the onunload event in javascript. Have a look at http://help.dottoro.com/ljflhicd.php for full details.
Refer these links. Sure this is a light for ur darkness.
Link1:
http://www.daniweb.com/forums/thread252828.html
Link2:
http://phpeasystep.com/phptu/9.html
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.