Jump to content

[SOLVED] Registered Users Online


wintallo

Recommended Posts

Hey,

 

Right now I'm programming a user authentication system in PHP and I have a question about a fun feature I want to add. I want to have one of those things that tells you if a user is logged on at the moment, such as the one on PHP Freaks. I was thinking that I should have a mySQL table dedicated solely to online users. I just don't know how (and where) I should put the script that deletes old users from the "online" table, say if the users didn't use the log out feature, but just closed his/her browser.

 

If you have any idea what I mean, I would appreciate some help. 

 

Thanks.

 

p.s. this is what I have so far.

-wintallo

Link to comment
https://forums.phpfreaks.com/topic/62199-solved-registered-users-online/
Share on other sites

You could use a crontab to execute a script to remove all users who in the active table haven't loaded a new page for x minutes. Having the crontab run every x minutes.

 

Or simply have a script which runs 1/3 page views or so that performs a query like

$wait = 600; //seconds to delete

$value = time() - $wait;

DELETE FROM user_active WHERE lastvisit<$value;

For archival purposes and so Trium918 can see how I did it, I will post my completed code. NOTE: this is untested because I haven't had the time :)

 

This is the script that is loaded on every page. It is meant to update the time if the user is logged in.

 

if ( confirm_user($_SESSION['username'], $_SESSION['password']) ) {
include 'database.php';
$query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['username'])."';";
mysql_query($query);
$query = "INSERT INTO online ( username, time ) VALUES ( '".mysql_real_escape_string($_SESSION['username'])."', '".time()."' );";
mysql_query($query);
mysql_close($database_connection);
}

 

 

 

This is inside the login script so that the user is logged as online when he or she dives the correct username and password (logs in).

 

		include 'includes/database.php';
		$query = "UPDATE users SET lastlogin = '".time()."' WHERE username = '".$_POST['username']."';";
		mysql_query($query);			
		mysql_close($database_connection);

 

 

 

This is inside the logout script, so it deletes the record of a user being online from the "online" table.

 

include 'includes/database.php';
$query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['username'])."';";
mysql_query($query);
mysql_close($database_connection);

 

 

 

This is the script that outputs what users are online. It's not pretty, but it works.

 

<?

include 'includes/include.php';

$seconds_to_wait = 600;
$threshold = time() - $seconds_to_wait;

include 'includes/database.php';
$query = "DELETE FROM online WHERE time < '".$value."';";
mysql_query($query);
$query  = "SELECT username FROM online;";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    echo $row['username']." ";
} 
mysql_close($database_connection);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.