Hi,
I'm working on a script that displays the number of registered users online and their usernames. Once the users log in i add their details into a db.
Here's the relevant code for login.php
$query = "SELECT user_id, first_name, username FROM users WHERE username='$u' and password=PASSWORD('$p')";
$result = @mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
if ($row) {
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
$_SESSION['username'] = $row[2];
$query = @mysql_query("INSERT INTO useronline (time, ip, server, username) VALUES (CURTIME(),'$REMOTE_ADDR','$PHP_SELF', '$row[2]')");
ob_end_clean();
header ("Location: http://" . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . "/index.php");
exit();
and here's the query in logout.php to delete the user from the db
$query = @mysql_query("DELETE FROM useronline WHERE ip='$REMOTE_ADDR'");
My first question is: if there are two users with the same ip and only one of them logout, then they will be both deleted from the db. So i want to make the query WHERE ip='$REMOTE_ADDR' and username='$username'. How can i retrieve the username from $_SESSION['username'] ?
My second question is: the script works fine as long as the users logout. but not all the users logout evarytime, most of them just close the browser, so their entry is still going to be in the db. Is there a way to delete those entries after a period of time?