I\'ve created a very simple multiuser game for learning purposes.
As users log on, each other user gets an update of all currently logged in users.
When a us
A possible way to only catch a user who comes back to the site after they closed the window could be using a database for user ID, IP and time like so:
$_SESSION['ip'] = $_SERVER['HTTP_CLIENT_IP'];
if(isset($_SESSION['ip'])){
if(Logedin){ // This login detection can be set by cookies can very depending on situation
mysql_query("INSERT INTO users SET user_login=\"".'1'."\",user_id=\"".$idfromlogin."\",user_ip=\"".$_SESSION['ip']."\",user_time=\"".time();."\"");
}
Then we can check this every time a page loads against the time();
if the user closes window by "X" button the session is over. Now we have to start a new session but that's why we saved the time in the database.
$_SESSION['ip'] = $_SERVER['HTTP_CLIENT_IP'];
if(isset($_SESSION['ip'])){
if (!$sql = mysql_query("SELECT * FROM users WHERE user_ip=".$_SESSION['ip'])){
die('Error: ' . mysql_error());
}
$row = mysql_fetch_array($sql);
if ($row['user_time'] + 10 * 60 < time()){ // this is 10 minutes have passed
// now to find the user database by the session database the logins is your method of loging in a user from logins database with pass and name
if (!$user = mysql_query("UPDATE logins SET user_isloggedin=\"".'0'."\" WHERE user_id=".$row['user_id'])){ //from the users database
die('Error: ' . mysql_error());
}
}
This only shows a way to check IP if they came back and the recorded time has passed previous. This does not show how to catch when some one presses "X" button. IP's can change so I would write a check IP function:
function getRealIpAddr(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check IP from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //to check IP is pass from proxy
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}