The problem is that people will move without logging out, their session will still exist for how long you will have a timeout set before their session data is collected (in fact, it may be much longer )
In order to get a truly accurate account of who has registered and visits the site, each client will send a "heartbeat" to the server every few seconds or minutes. at each heart rate trigger, you would like to expire any users who did not check within the allocated time interval.
The heartbeat signal is probably best composed of a username and a timestamp at a minimum, but may include any information you want to track.
, , , . , . , , , . 30 , .
(, , , )
, , SELECT * FROM table, .
:
, heartbeat .
var Client = Client || {};
Client.Pulse = null;
$(document).ready(function(){
Client.Heartbeat();
});
Client.Heartbeat = function(){
var sUsername = 'SomeUser';
$.ajax({
url : '/lib/server/Heartbeat.php',
type : 'post',
dataType : 'json',
data : {Username : sUsername },
success : function(jActiveUsers,sStatus,jqXHR){
Client.RenderActiveUsers(jActiveUsers);
Client.Pulse = setTimeout(function(){
Client.Heartbeat();
},30000);
}
});
}
Client.RenderActiveUsers = function(jActiveUsers){
}
setTimeout(), , setInterval(), , , . setTimeout() ; , ( , ).
:
, Java , , , PHP; , . MySQL. PHP (ish)
. , , , ( , , , )
:
ActiveUsers
-----------------------------------------
| Field | Type | NULL | Key |
-----------------------------------------
| Id | int | | PRI |
| Username | varchar(xx) | | |
| LastPulse | datetime | | |
| FirstPulse | datetime | | |
-----------------------------------------
() , , PHP, Java Sessions, , , . PHP , , , (, , .)
, , , -, . , ....
, , , .
if($LoggedIn && ($Username != null)){
, , .
,
SELECT `Id` FROM `ActiveUsers` WHERE `Username` = '{$Username}' LIMIT 1
, , , , Id,
UPDATE `ActiveUsers` SET `LastPulse` = NOW() WHERE `Id` = {$Id}
,
INSERT INTO `ActiveUsers` (`Username`,`LastPulse`,`FirstPulse`) VALUES ('{$Username}',NOW(),NOW())
, , , , (2 ).
DELETE FROM `ActiveUsers` WHERE `LastPulse` < (NOW() - INTERVAL 2 MINUTE)
ActiveUsers , , , , , ,
SELECT `Username`, UNIX_TIMESTAMP(`FirstPulse`) AS `FirstPulse` FROM `ActiveUsers` ORDER BY `FirstPulse` ASC
( PHP) , JSON json_encode() print() ed "application/json", jQuery . , Java , " , JSON " .
, . , .
100% - , . , .
( , Wall-O-Text)