LemonInflux Posted October 6, 2007 Share Posted October 6, 2007 Ok, this is half logic, half code help. Basically, I can figure out how to add people to the list. I'd presume you'd have a column in a database table called on/off. In them, you could either have 1 or 0. If someone's online, 1, if not, 0. I'd guess that the 1 would be written as they log in and the sessions are registered. But, how do you write the 0? At what point is that written? So basically, how would you do it, and code if possible? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/ Share on other sites More sharing options...
pocobueno1388 Posted October 6, 2007 Share Posted October 6, 2007 The only problem with your method is you can't rely on the user to logout. The logout page would be where you set it to 0...but thats not going to work as most people just go to the next website without logging out. What you have to do is make a column in the users table called "last_active" or something like that. Every time they click, you need to update that column with a timestamp. Then, to display the online list, your query would look like this: SELECT username FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE) ORDER by userID ASC That will grab everyone who has clicked on your webste in the last 5 minutes (the users logged in at least) Hopefully that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363571 Share on other sites More sharing options...
LemonInflux Posted October 6, 2007 Author Share Posted October 6, 2007 So erm...how, in terms of real php, would I go about this? As in, adding a readable timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363572 Share on other sites More sharing options...
pocobueno1388 Posted October 6, 2007 Share Posted October 6, 2007 Call this code on every page...so if you have a header page, put it there. <?php $query = mysql_query("UPDATE users SET last_active=NOW() WHERE userID='$userID'")or die(mysql_error()); ?> Now put this on the page where you want to display the users online <?php $query = "SELECT username FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE)"; $result = mysql_query($query)or die(mysql_error()); while ($row = mysql_fetch_assoc($result)){ echo $row['username'].'<br>'; } ?> Obviously your going to have to change up the queries to match your database. Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363577 Share on other sites More sharing options...
LemonInflux Posted October 6, 2007 Author Share Posted October 6, 2007 Sorry, wasn't clear. I understood that bit, it was the timestamp bit I Was asking about. Not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363579 Share on other sites More sharing options...
pocobueno1388 Posted October 6, 2007 Share Posted October 6, 2007 In your phpmyadmin create a field called "last_active" and set it to a datetime type. Then when you run this query UPDATE users SET last_active=NOW() WHERE userID='$userID' It will update that datetime field to the current time. Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363582 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 I agree with pocobueno1388 way of going about this, but if your having trouble doing that. Then you could simply do the way you suggested the on/off field in the database. They login, 1 is inserted into the database. Logout page, update it to 0. And each page you can check if the session variables exist. If they don't update it to 0. And thats not to say, a better way to do this then that, would be. When they log in, create a session variable for their user id. and another session variable for on/off , then if they close their browser or dont logoff, you can time them out. And you will still be able to work with the database cause you have their id. I hope this helps. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363659 Share on other sites More sharing options...
JasonLewis Posted October 7, 2007 Share Posted October 7, 2007 i do basically the same as pocobueno... except i don't use the datetime, i do it like this... the database would have these 2 fields, online and last_active (last_active is a varchar) then on the login page you would have this: mysql_query("UPDATE `users` SET `online`='1', `last_active`='".time()."' WHERE `username`='{$username}'"); all that does is sets the user to online and sets the last_active to the current time. now you need a page that will be included on every page, so that when a user access a page the included script is run.. so in the included script you'd have this: $session_timeout = 5; //in minutes $timeout = time() - ($session_timeout * 60); //if any users with a timeout equal to or less then this number, they get signed out. if(isset($_SESSION['member_session'])){ //check if your member login session is running, if it is, a member is logged in. $last_update = mysql_result(mysql_query("SELECT `last_active` FROM `users` WHERE `username`='{$username}'"),0); //grab the last update for this user. if($last_update <= $timeout){ //check the update, if its less then the timeout then we sign them out. mysql_query("UPDATE `users` SET `online`='0', `last_update`='' WHERE `username`='{$username}'"); session_destroy(); //sign the user out }else{ //if its not, we update there last_update to the current time mysql_query("UPDATE `users` SET `last_update`='".time()."' WHERE `username`='{$username}'"); } } first this script creates the variables for timing out. then it checks if the user is online, you can add an else statement to this to add in for guests if you want. Then it grabs the users last update from the table and checks it against the timeout variable. If the if statement passes, it sets the users online value to 0 and destroys the session. if the statement fails, it updates the database setting the last_update to the current time. hope this gets you motivated! Quote Link to comment https://forums.phpfreaks.com/topic/72121-simple-whos-online-list/#findComment-363691 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.