bcooperz Posted August 30, 2011 Share Posted August 30, 2011 Hey Guys/Girls, Thanks for offering to help! I'm currently setting up a small social network for school and I just basically want to know whether the way I'm dealing with in-active users is appropriate and not going to SLOW down my code A LOT, My method is : 3 Functions: function update_active_user(){ global $connection; global $id; $time = time(); $result3 = mysql_query("UPDATE users5 SET last_update = '$time' WHERE id = '$id'", $connection); } function update_inactive_users(){ global $connection; $time_to_expire = time() - 300; // 300 seconds off the current time $result2 = mysql_query("UPDATE users5 SET online='0' WHERE last_update < $time_to_expire AND online='1'", $connection); } function update_active_users(){ global $connection; $time_to_expire = time() - 300; // 300 seconds off the current time $result2 = mysql_query("UPDATE users5 SET online='1' WHERE last_update > $time_to_expire AND online='0'", $connection); } First function updates the user's field called last_update to the current time (This will only occer when the script loads and the user has done something on my website, it will update their last_update field to current time) Second function sets ALL users that haven't loaded any pages in the last 300 seconds to offline or field online to 0, which obviously means the user is offline Third function sets ALL users that have loaded pages in the last 300 seconds to online if they're offline The reason I'm worried about this is not because it doesn't work, it's working fine at the moment with 2 users, I'm worried when their might be A LOT of users and there are people who are active. My question is : will it make my scripts really slow or be bad for my database in ANY way if I do use these functions for ALL users because each time it searches the table, it is searching ALL users to see if they're active or inactive. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/ Share on other sites More sharing options...
AyKay47 Posted August 30, 2011 Share Posted August 30, 2011 what about users that are on your website and currently doing something else atm.. meaning they are inactive for more than 30 seconds but still on your website/online..? Also, I wouldn't recommend using global calls to variables.. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263464 Share on other sites More sharing options...
bcooperz Posted August 30, 2011 Author Share Posted August 30, 2011 what about users that are on your website and currently doing something else atm.. meaning they are inactive for more than 30 seconds but still on your website/online..? Also, I wouldn't recommend using global calls to variables.. It's 300 seconds not 30 seconds and the globals are not going to change regardless so I'm not to worried about that, also, the user isn't logged out if the field says they are offline, it will just say they're offline, as soon as they do something it will say they're back online. I really just want to so if it's safe to use these functions with a lot of users, will it slow down my website ETC? Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263467 Share on other sites More sharing options...
bcooperz Posted August 30, 2011 Author Share Posted August 30, 2011 Can someone please help me Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263473 Share on other sites More sharing options...
Adam Posted August 30, 2011 Share Posted August 30, 2011 You can drop two of those functions; you know if the user is online or offline based on their last_active stamp. Running two separate updates (requiring full table scans unless you add an index on the online column) on every request, to update a column which is essentially duplicate data, doesn't make much sense. It's like storing a date of birth and age. Plus in one of your queries you're scanning the table with the WHERE condition you would need to populate the list of online users anyway. Edit Don't take that to mean "add an index on the 'online' column" - drop it. Work out users purely based on their last active stamp. Don't add an index to that either, as you'll be constantly updating it which defeats the purpose of an index. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263476 Share on other sites More sharing options...
bcooperz Posted August 30, 2011 Author Share Posted August 30, 2011 You can drop two of those functions; you know if the user is online or offline based on their last_active stamp. Running two separate updates (requiring full table scans unless you add an index on the online column) on every request, to update a column which is essentially duplicate data, doesn't make much sense. It's like storing a date of birth and age. Plus in one of your queries you're scanning the table with the WHERE condition you would need to populate the list of online users anyway. Edit Don't take that to mean "add an index on the line column" - drop it. Work out users purely based on their last active stamp. Don't add an index to that either, as you'll be constantly updating it. Ok, so what your telling me to do here is drop the 'online' column and purely check the user's timestamps to see how long ago they were active Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263478 Share on other sites More sharing options...
Adam Posted August 30, 2011 Share Posted August 30, 2011 Yep. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263482 Share on other sites More sharing options...
AyKay47 Posted August 30, 2011 Share Posted August 30, 2011 i still don't get the 5 minute rule.. seems inconvenient.. depends what functionality you are going to have I guess.. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263491 Share on other sites More sharing options...
Adam Posted August 30, 2011 Share Posted August 30, 2011 Five minutes may be a bit short. You'll end up with people flicking on and off line too often when they're reading or writing; but that's a minor detail you can experiment with. Quote Link to comment https://forums.phpfreaks.com/topic/246020-user-online-offline-status/#findComment-1263512 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.