Jump to content

Storing A User's Online Status


kelliinpb

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.