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
Share on other sites

Have a piece of code on each page that updates a field in the users table with the current datetime, then in your "Currently Online" box, select all users from the table that have accessed a page within the last n minutes.

Link to comment
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;

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.