1

Using ajax, I made it possible so that a user gets logout after 10 seconds of staying on the same page. Keep in mind that the 10 seconds is just for testing purposes. However, if the user closes their browser before the 10 seconds are up, they do not get log out. I thought about logging them out when they exit their browser, but I have yet to figure out how, and even if I do, what does exiting their browser entails? Does it count if they just close a tab while there are other tabs open? What if they have 2,3 or more browsers open at the same time? How do I determine that all those open browsers are exited before logging them out?

test1.php

<?php

include('ajax.php');
sleep(10);

?>

<html>
<body onload = "ajax('test2.php','output',10000)">
<div id="output" />
</body>
</html>

test2.php

<?php

include('connect.php');
include('functions.php');

ud("login = 'false'/userLogin/where username = '$username'");

?>
4
  • Do you think the unload method would work for you? api.jquery.com/unload Commented Jun 5, 2015 at 18:48
  • Scratch that I think it was deprecated. onBeforeUnload should still be good however. w3schools.com/jsref/event_onbeforeunload.asp Commented Jun 5, 2015 at 18:49
  • @wrxsti What does exiting their browser entails? Does it count if they just close a tab while there are other tabs open? What if they have 2,3 or more browsers open at the same time? How do I determine that all those open browsers are exited before logging them out? Commented Jun 5, 2015 at 18:51
  • Added an answer with that info. :) Commented Jun 5, 2015 at 18:54

1 Answer 1

1

The onBeforeUnload event should work for you.

It covers:

  • Closing a tab (regardless of how many tabs are open)
  • Exiting the browser
  • Navigating away from the page
  • Refreshing the page

You can use this as inline attributes, or to trigger ajax functions to notify the server. There are several methods to accomplish what you are looking for, however I must note I have never attempted what you are trying. I am only speaking from what I have read about it.

Here are some other sources on SO as well.

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

I need to check whether they've been active in the last 10 seconds. I can use AJAX to check if they've been active in the last 10 seconds. If they navigates, or refreshes the page, then it resets the counter, and let me know that they've been active in the last 10 seconds. However, if they do nothing in 10 seconds, they will be automatically logged out.
If they closed their browser, or exit the tabs that monitors their progress using AJAX, it will no longer monitor their progress, and thus their counter will not be updated, and thus I will have no idea whether they've been active or not. I can't just log them out if they close a tab or a browser, because what if they have multiple tabs or browsers of my website open? Then I would only want to log them out when they closed all of them.
That may be quite the task, but this will likely be the start to that solution. Sadly I'm on my way home but when I get there I will try to build a test platform along the lines of what you are trying yo accomplish. I'll update when I have more if nobody comes up with a solution faster.
Would greatly appreciate it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.