wintallo Posted July 28, 2007 Share Posted July 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 28, 2007 Share Posted July 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
lightningstrike Posted July 28, 2007 Share Posted July 28, 2007 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; Quote Link to comment Share on other sites More sharing options...
wintallo Posted July 28, 2007 Author Share Posted July 28, 2007 Okay, thanks for the replies! I'll try those out, that answers my question so I will make this thread "SOLVED" Quote Link to comment Share on other sites More sharing options...
wintallo Posted July 29, 2007 Author Share Posted July 29, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.