steviez Posted June 9, 2008 Share Posted June 9, 2008 Hi, On my site when someone logs in my script runs a simple query to change their status to online. The problem i am having is that if users forget to log out then they are constantly displayed as online. How would i stop this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/ Share on other sites More sharing options...
Clinton Posted June 9, 2008 Share Posted June 9, 2008 Sounds like you would probably want to query the session to see if it is still active. Then base it off on that. Not sure though. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561241 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 Easiest way to have a column associated with the user that gets updated with the current time, every time they make a request to the server (click a link, submit something, etc..) and then setup a cron job script and run it every x time (like 15 mins or so..however long you want to deem "inactivity" to be considered "logged out") to check the current time with the last active time of users and if it's greater than your x time, flag their status as offline. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561242 Share on other sites More sharing options...
Clinton Posted June 9, 2008 Share Posted June 9, 2008 Like I said. Column with time, Cron Job. Is there an Echo in here? Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561244 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 Like I said. Column with time, Cron Job. Is there an Echo in here? Umm, that's not at all like what you said. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561252 Share on other sites More sharing options...
Clinton Posted June 9, 2008 Share Posted June 9, 2008 I was kidding, hence the wink and the little laughing man. I did almost say cron it though. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561254 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 Ah damn, I don't know how I missed that. I fail. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561258 Share on other sites More sharing options...
steviez Posted June 9, 2008 Author Share Posted June 9, 2008 Ah damn, I don't know how I missed that. I fail. LMAO OK... I can make the system update the current time stamp but how do i get it to compare it with the current time to see if its expired? Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561262 Share on other sites More sharing options...
Clinton Posted June 9, 2008 Share Posted June 9, 2008 You'll have to use strtotime and then do equation. Basically if columntime > strtotime(columtime, +15 minutes) then update and set online status to offline. (My strtotime isn't perfect I'm recalling it off the top of my head but that's the general idea) Use the cron job to run the php file every 15 minutes after that. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561265 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 well the time() function returns the current unix timestamp in seconds so if you take the current timestamp in your db and format it to that, you can do a simple if (($nowtime - $dbtime) > 300) { // 300 is in seconds so that's 5 mins // change user status } Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561269 Share on other sites More sharing options...
abdfahim Posted June 9, 2008 Share Posted June 9, 2008 what I did for my website is when ever any user click any link, I update his online status as 1 and also update his last login time column. Then in a header page (which is included in all pages), I wrote code to set online status 0 to those whose last login time is greater than 5 mins (dats my threshold). So, any user click any link to my site hence update the current online user tally. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561273 Share on other sites More sharing options...
NorthWestSimulations Posted June 9, 2008 Share Posted June 9, 2008 What i did to my site is when they login it changes the status of the mysql table `online` to `online` = 'yes' and when they logout it changes it to `online` = 'no'. Im sure theres some way to pull the info from where the sesions data is stored but Im not sure how Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561294 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 Yeah that's an alternative but thing is, that creates a lot more unnecessary load for your server, and is also dependent on users clicking it. What happens when you have a dry spell and nobody's there clicking? I guess that kind of falls under the "If a tree falls in the forest and noone is there, does it make a sound?" category: that is, it doesn't matter if it's showing people as "online" if nobody is there to see it...but still. But hey, if you're happy with that, then go for it. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561297 Share on other sites More sharing options...
abdfahim Posted June 9, 2008 Share Posted June 9, 2008 For Crayon Violent ... I totally agree with you about the server load, it do actually create unnecessary load on server. But the thing is I know there will be at best 10 ppl at a time in my server. Another major reason is I have windows server and I know very little about running Cron Job running on a windows server... may be Schedule Task would work, but I never tried. Nut I totally oppose ur Forest and Tree theory. I dont know about others, but my goal for having online user function is there so that the viewers can see who are online now. So, if there is nobody there to click any link, what matters if online user count is right or wrong... nobody is going to check that from phpmyadmin !! whenever any user comes to the page, the online user count will be corrected. So nobody ever can see any wrong info there in the page. For NorthWestSimulations... What i did to my site is when they login it changes the status of the mysql table `online` to `online` = 'yes' and when they logout it changes it to `online` = 'no'. Im sure theres some way to pull the info from where the sesions data is stored but Im not sure how what happens if user A logs in and then cross the window without logout (I think most user do that unless it is bank account or so)? Then user A will remains online forever unless he comeback and click on logout ... believe me, the chance is very little... Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561326 Share on other sites More sharing options...
steviez Posted June 9, 2008 Author Share Posted June 9, 2008 well the time() function returns the current unix timestamp in seconds so if you take the current timestamp in your db and format it to that, you can do a simple if (($nowtime - $dbtime) > 300) { // 300 is in seconds so that's 5 mins // change user status } I went with this way, and my code is as follows: <?php // Update online users last active, If inactive change status to offline $dbtime = sql_row("SELECT * FROM members WHERE status = 'Online' AND username = '".$_SESSION['gt_username']."'"); $lastactive = $dbtime['lastactive']; $nowtime = time(); if (($nowtime - $lastactive) > 300) { // 300 is in seconds so that's 5 mins mysql_query("UPDATE members SET status = 'Offline' WHERE username = '".$_SESSION['gt_username']."'"); } ?> Thing is its not working :S Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561332 Share on other sites More sharing options...
corbin Posted June 9, 2008 Share Posted June 9, 2008 Orrr.... You could just store the last active time in the database, and not worry about CRON or anything. (Although, if you have a lot of users, a crontab would be better as far as server stress goes.) Then, all you do, is update the timestamp each page load... UPDATE users SET last_active = UNIX_TIMESTAMP() WHERE user_id = <user_id> Then, to find all of the online users (active within 10 minutes), you could just do... SELECT * FROM users WHERE (last_active > UNIX_TIMESTAMP()-600) Or, to find out if a user is online... SELECT last_active FROM users WHERE user_id = <user_id> Then, pull last_active into a variable.... Then... if(last_active > time()-600) But this is potentially a tiny bit more stressful than a crontab. (Gotta go now, but I can explain it later if you want.) Anyway, that's just another way to do it. Not saying it's the way I would do it (although I would consider it), but it's just another possibility. Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561335 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 did you echo out $lastactive and $nowtime to see if they are holding what you expect? Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561338 Share on other sites More sharing options...
steviez Posted June 9, 2008 Author Share Posted June 9, 2008 did you echo out $lastactive and $nowtime to see if they are holding what you expect? Hi, It works if i execute it manually but the cron wont work. Here is what i have: 5 * * * * /home/mydir/cron/online_users.php Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561369 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 how are you doing this cron job? on the command line? through cpanel? Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561382 Share on other sites More sharing options...
steviez Posted June 9, 2008 Author Share Posted June 9, 2008 cpanel Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561386 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 The command needs to look something like this: GET http://yourdomain.com/path_to_file/file.php > /dev/null Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561387 Share on other sites More sharing options...
steviez Posted June 10, 2008 Author Share Posted June 10, 2008 When using cron i get this error: <br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent in <b>/home/mydir/public_html/cron/online_users.php</b> on line <b>14</b><br /> <br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/mydir/public_html/cron/online_users.php:14) in <b>/home/mydir/public_html/cron/online_users.php</b> on line <b>14</b><br /> My code is this: <?php // Start The Session session_start(); // INCLUDE DATABASE INFORMATION include "../include/database_config.php"; // INCLUDE CLASS/FUNCTION FILES include "../include/class_database.php"; include "../include/functions_general.php"; include "../include/global_config.php"; // INITIATE DATABASE CONNECTION $database = new se_database($database_host, $database_username, $database_password, $database_name); // ENSURE NO SQL INJECTIONS THROUGH POST OR GET ARRAYS $_POST = security($_POST); $_GET = security($_GET); // Update online users last active, If inactive change status to offline $dbtime = sql_row("SELECT * FROM members WHERE status = 'Online' AND username = '".$_SESSION['gt_username']."'"); $lastactive = $dbtime['lastactive']; $nowtime = time(); if (($nowtime - $lastactive) > 300) { // 300 is in seconds so that's 5 mins mysql_query("UPDATE members SET status = 'Offline' WHERE username = '".$_SESSION['gt_username']."'"); } ?> Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-561666 Share on other sites More sharing options...
steviez Posted June 11, 2008 Author Share Posted June 11, 2008 got it sorted now all i did was remove the session_start() and just query the database for all online users that were idel. Thanks for al your help guys! Quote Link to comment https://forums.phpfreaks.com/topic/109417-solved-users-online-help/#findComment-563231 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.