-2

I have an app where a user absolutely must be logged in to use it.

I use PHP sessions upon login to store user data.

If a user leaves a window open and then returns e.g. an hour later, what's the best way to check if they are logged in still?

Options I can think of are:

1) check mouse movement which then fire AJAX php file to see if session still active

2) check as above on mouse click anywhere

But these will then be running constantly and therefore hitting and hitting the server potentially thousands of times a second with not so many users, so I doubt efficient.

Is there a better way to do this? Is there a 'standard'?

I am using PHP and JQuery.

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • 1
    "logged in" and active are not the same thing. are you saying you have to know if they are 'active' ?? –  Nov 03 '13 at 22:09

1 Answers1

2

Use Javascripts setInterval(function, time) function to poll the server every X number of minutes to check if the user is authenticated. Return a response in JSON based on said result.

See javascript setInterval for a good discussion about the setInterval() function.

If you are in fact asking about a user being active on the website, you could use the setTimeout() function after X amount of minutes (such as an hour) to log the user out via AJAX request, or display a warning such as the ones in online internet banking.

If a user makes a request before the timeout occurs then this process is restarted naturally due to the request - response cycle.

Community
  • 1
  • 1
SamV
  • 7,548
  • 4
  • 39
  • 50