kelliinpb Posted March 15, 2008 Share Posted March 15, 2008 Hello everyone, I apologize in advance if this has already been asked, I tried searching and couldn't quite find what I was looking for. Here is my objective: to store a user's online status so when they log into their account, it displays in their profile that they are currently online. When they logout or close the browser, it would change that display to 'Online Today'. I would like to show if they are online now, today, in the past week or in the past 30 days. I have seen this on various sites but have yet to find out exactly how they do it. If they were to simply logout by clicking a button, that would be easy enough but as we all know, that is not the case. If anyone can help me I would greatly appreciate it. Quote Link to comment Share on other sites More sharing options...
derrick1123 Posted March 15, 2008 Share Posted March 15, 2008 We need some code to work with... Its like baking a cake with only having a pan and an oven. >.< Quote Link to comment Share on other sites More sharing options...
kelliinpb Posted March 15, 2008 Author Share Posted March 15, 2008 Ok...after a user logs in (and assuming their login is successful) before redirecting to the user's home page, I would add in a script to add the current date and time and wipe out the previous logout date and time so it would look something like this: [pre] <?php $currentdate = date("Y-m-d"); $currenttime = gmdate("H:i:s"); $query = "UPDATE onlineStatus SET loginDate = '$currentdate' AND loginTIme = '$currenttime' AND logoutDate = '' AND logoutTime = ''"; $result = mysql_query($query) or die(mysql_error()); ?>[/pre] If the user were to simply click on the logout link, then I wouldn't have a problem as I could easily update the logout date and time. However, if a user does not, then I am not sure how to do this if they close the browser or the session times out. If you can help me, I would really appreciate it. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 15, 2008 Share Posted March 15, 2008 whenever a user does anything (views a page, anything on the server that executes PHP), update their record with the time(). to see if a user is on line, look to see if they have done anything in X amount of time. maybe X could be your session length. if they haven't done anything in X amount of time, they are no longer logged on. Quote Link to comment Share on other sites More sharing options...
kelliinpb Posted March 16, 2008 Author Share Posted March 16, 2008 I took a look through my cookies and saw a cookie from a website that does what I am looking for. I've never used cookies before, would I be able to do this somehow with cookies? Quote Link to comment Share on other sites More sharing options...
kelliinpb Posted March 16, 2008 Author Share Posted March 16, 2008 If it would help, I could send you an e-mail with the site I'm looking at that does this, a part of the page code and the info from the cookie the site placed on my computer. Let me know if you think you can help me out. Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2008 Share Posted March 16, 2008 Cookies are not going to do anything to help you in this situation as they are stored client-side. You need to do as BlueSkyIS explained. On any page access update the user record with a timestamp. Then, you need to decide at what time period the user is no longer "logged in", because you have no way of knowing if the user closed their browser window and shut down the computer or if they are just takign their time reading the content on a page. Personally, I would go with 20 minutes. So, if the user's last activity was within 20 minutes then you show them as "online". Otherwise, if their last activity was today (but not within 20 minutes) then you would show them as logged in today. Etc. Quote Link to comment Share on other sites More sharing options...
kelliinpb Posted March 17, 2008 Author Share Posted March 17, 2008 That would work, but what if they close the browser out? I don't have any experience running a cron job, but wouldn't that be taxing on the server to run a job every 20 minutes on a site that could potentially have tens of thousands of users? Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 17, 2008 Share Posted March 17, 2008 If they close the browser their 'last click' timestamp (as I call it) will not update, and will soon be more than 20 minutes old which will mean they're logged out. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2008 Share Posted March 17, 2008 That would work, but what if they close the browser out? I don't have any experience running a cron job, but wouldn't that be taxing on the server to run a job every 20 minutes on a site that could potentially have tens of thousands of users? There is absolutely no way to ever know if a user has closed the browser. You can supply a logout button/link, but many, if not most, users will not use it. They wil instead close the browser window when they are done. As I already stated before you can't know if the user has closed the browser window or if they are simply still viewing the last page accessed. From the server side you can only know the last accessed time of the user. That is why YOU need to set a time limit to determine if the user is still online. Quote Link to comment Share on other sites More sharing options...
kelliinpb Posted March 18, 2008 Author Share Posted March 18, 2008 I get it, thanks for all your help, if I happen to run into any problems, I will let you know but I think this will work great. 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.