plznty Posted May 23, 2009 Share Posted May 23, 2009 Using SQL databases and PHP. What is the best way for php code to pick up exactly whos logged into your website. Any suggestions would help. Just need rough idea. thank you, Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/ Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 Update a userid and timestamp with every page request. I made a system like this a while ago Check it out - http://www.jackpf.co.uk. Somewhere around the middle it'll say who's online, how many strangers etc. It also updates the location of the user, so I can figure out who's viewing what thread on the forum. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840389 Share on other sites More sharing options...
plznty Posted May 23, 2009 Author Share Posted May 23, 2009 Update a userid and timestamp with every page request. I made a system like this a while ago Check it out - http://www.jackpf.co.uk. Somewhere around the middle it'll say who's online, how many strangers etc. It also updates the location of the user, so I can figure out who's viewing what thread on the forum. Clever but how long until they time out, for example like how long would it take for there name 2 be removed if they like canceled internet explorer. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840390 Share on other sites More sharing options...
jkewlo Posted May 23, 2009 Share Posted May 23, 2009 well as soon as there cookie/session expires then it would remove them, I use a simple method of online 1 being yes and no being 0 when they log in it set's online to 1 in the database and when they logout sets 0 to the database thus removing them from the who is online pretty simple method <?php $sql="SELECT * FROM users WHERE online = 1"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ echo "Username: ". $rows->online ."; } ?> Hope this helped you Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840519 Share on other sites More sharing options...
deepson2 Posted May 23, 2009 Share Posted May 23, 2009 jkewlo, I am working on the same module. as you said once user is logged in,we can update coulmn from 0 to 1. but what for logout? i mean in my logout page only this code is there unset($_COOKIE['userid']); $update = $op->runsql("UPDATE ".tbl_author." SET status = 0 WHERE id = '$userid'"); unset($_COOKIE['lastpath']); unset($_COOKIE['lasturi']); session_destroy(); but this updation is not working for me. where you update your 1 to 0 status while logging out or on the log out page? please help me. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840530 Share on other sites More sharing options...
jkewlo Posted May 23, 2009 Share Posted May 23, 2009 Logout ~ $sql1="UPDATE users SET online=0 WHERE username='". $username ."'"; $result1 = mysql_query($sql1); Login ~ $sql1="UPDATE users SET online=1 WHERE username='". $username ."'"; $result1 = mysql_query($sql1); Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840538 Share on other sites More sharing options...
deepson2 Posted May 23, 2009 Share Posted May 23, 2009 jkewlo, you must have two different pages like login.php and logout.php right? so on login.php my status is getting changed properly. but not on logout.php do you update your both the query on same page or like me on two different pages? Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840543 Share on other sites More sharing options...
BK87 Posted May 23, 2009 Share Posted May 23, 2009 for true user online, you would need to update the db each time a page loads to the perticular user... because just because you changed "online" to 1 (for logged in) dosen't mean that if I close my web browser I will get logged out in 10-20 mins. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840545 Share on other sites More sharing options...
deepson2 Posted May 23, 2009 Share Posted May 23, 2009 BK87, thats what i was thinking and asking here how can jkewlo done that so easily. well you can see my thread and what i am upto here http://www.phpfreaks.com/forums/index.php/topic,253569.0.html please help me. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840574 Share on other sites More sharing options...
BK87 Posted May 23, 2009 Share Posted May 23, 2009 jkewlo only shows how to change user status... not actually follow the user.. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840577 Share on other sites More sharing options...
redarrow Posted May 23, 2009 Share Posted May 23, 2009 read this example provided via a expert. http://www.phpfreaks.com/forums/index.php?topic=223367.0 Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840579 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 Yeah, but the problem with updating a database when users log in/out is that they might not necessarily log out. They could just close their browser which would result in them being displayed as online well....forever. If you follow my method you avoid that, as you can time them out. I fetch all users with a unix stamp >= time() - 600, which is 10 minutes. So if a user hasn't requested a page in the last ten minutes, they're considered offline. The ajax solution is basically the same, except it will check if the user is viewing pages as well. However, this would put a lot of strain on the server. Obv the more you update the database, the slower it's going to be. But yeah, that's how I did it. Works well for me. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-840626 Share on other sites More sharing options...
jkewlo Posted May 24, 2009 Share Posted May 24, 2009 well true to all of you, I use a session so I destroy it after 3 minutes of inactivity. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-841033 Share on other sites More sharing options...
jackpf Posted May 24, 2009 Share Posted May 24, 2009 3 Minutes?!?! So if someone logs in and goes for a crap, they come back and have to log in again? Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-841123 Share on other sites More sharing options...
plznty Posted May 25, 2009 Author Share Posted May 25, 2009 3 Minutes?!?! So if someone logs in and goes for a crap, they come back and have to log in again? It will keep connecting to page which updates, if the page doesnt update within 3 minutes a timeout occurs. Quote Link to comment https://forums.phpfreaks.com/topic/159336-best-solution-for-users-online/#findComment-841431 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.