I am working on an online system at the moment that currently included a registration and log in system. Now i also want to include a Users online section which is giving me trouble.
Currently when a user logs in, once the script has authenticated them it will get their user name then log the details into an "online" database table like so:
if($row['Activated'] > 0)
{
$_SESSION['s_logged_n'] = 'true';
$_SESSION['s_username'] = $username;
$_SESSION['s_name'] = $row['Name'];
$insertuser="INSERT INTO online(Username) values('$username')";
mysql_query($insertuser) or die("Could not login insert user");
header("Location: index.php");
}
else
{
Then in my information center i just pull all results from the "online" table to display the users online.
Now off course by using this method the only possible way a name can be removed from the database is if the user manually hits the logout button where i then run:
$username = $_SESSION['s_username']; $query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';"; $result = mysql_query($query); $_SESSION['s_logged_n'] = ''; $_SESSION['s_name'] = ''; $_SESSION['s_username'] = ''; session_destroy();
If a user just closes the browser without logging out they will stay logged in for the default php session length before the session is destroyed. The problem here is that it won't remove them from the online table meaning they will always be displayed in users online. When they log in next their name will then appear 2 times in users online and only get taken off if they actually click log out.
And to display them in the users online section i use:
$online = mysql_query("SELECT username FROM online") or die(mysql_error());
if(mysql_num_rows($online) > 0)
{
echo '<tr class="mainrow"><td>Currently active: ';
while($row = mysql_fetch_assoc($online))
{
$onlineusers[] = '<A href="member_profile.php?username='.$row['username'].'">'.$row['username'].'</a>';
}
echo implode(', ' , $onlineusers) . ' </td></tr>';
}
else
{
echo '<tr class="mainrow"><td>There are currently no users logged in</td></tr>';
}
I am quite bad at sessions but i need to change this and cause the user to get logged out via the session if they have been inactive for 15 minutes which will destroy their session and also remove them from the online list.
I am struggling to get my desired result after a lot of testing so some help would be greatly appreciated.
Currently i have this which is run on each page to check if a user has been active within 15 minutes. Only problem is it doesn't seem to work. As soon as a user logs in, it automatically removes them on the page load after hitting log in.
session_start();
if ($_SESSION['last_active'] <= (time() - 900)) // 900 = 15 minutes (60x15)
{
$username = $_SESSION['s_username'];
$query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';";
$result = mysql_query($query);
$_SESSION['s_logged_n'] = '';
$_SESSION['s_name'] = '';
$_SESSION['s_username'] = '';
session_destroy();
header("Location: index.php");
}
else
{
$_SESSION['last_active'] = time();
}
So my current dilemma, how can i get the above code to work and how i would i properly register the session upon logging in to add them into a database with the current time?
Also how would i show them in the online list once i get it working?
This post has been edited by Jye: 05 April 2010 - 06:37 AM

New Topic/Question
Reply




MultiQuote








|