How do you auto-logout someone?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you auto-logout someone?

Post by simonmlewis »

Hi

I have a simple script that when someone logs in, it stores their cookie, and marks them as 'online'. Then customers who need help from that person can see that at least one person is "online".

The problem I'm facing, is that this person tends to close their browser or their computer without logging off, and thus their status remains' online'.

HOWEVER.... when they go to login next, it won't let them login because their status is already online, the system won't let two people login with the same one set of credentials (security), and since their cookie has timed out, they must login in order to use it.

So... how do you have a script running in the background somehow, that updates their status in the DB to "offline"?

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: How do you auto-logout someone?

Post by Grizzzzzzzzzz »

a quick example, you could have a polling event occurring in the background, updating a timestamp field every 10seconds. If there hasn't been an update in the past 15seconds, treat that user as offline, if there has been, they're online. That way if there's a powercut, someone closes a browser or turns of their computer without logging out, the polling event no longer happens, so the timestamp field associated with that user doesn't get updated, so they are then treated as offline.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you auto-logout someone?

Post by requinix »

Do they have to remain logged in as long as the browser window stays open?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you auto-logout someone?

Post by simonmlewis »

Yes, because they will click away from a screen to view another, like list of conversations or something else. I did try a closing window script but caused a lot of harm.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you auto-logout someone?

Post by simonmlewis »

To Grizzzzzzzzzz
I don't know how to do that polling thing.
I know how to run PHP actions on command, but not things in the background. This could be very useful to know, as it would help to monitor web sites to ensure they are active as well.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: How do you auto-logout someone?

Post by Grizzzzzzzzzz »

simonmlewis wrote:To Grizzzzzzzzzz
I don't know how to do that polling thing.
I know how to run PHP actions on command, but not things in the background. This could be very useful to know, as it would help to monitor web sites to ensure they are active as well.
Keep in mind that this is only really a light weight solution as well, but relatively easy to do using Javascript / JQuery (assuming you've used them before)

http://api.jquery.com/jQuery.post/, that will come in handy

anyway roughly what you need:

- JS that polls a PHP file every X amount of seconds
- PHP file that handles polling & updating timestamps (you'll need an ID here to identify people, IP? Session Token? Security is something to consider)

If you really don't have a clue about polling, here's a very quick example I cobbled together

Code: Select all

<?php

if (isset($_POST['uid']))
{
  echo "you were online at " . date('H:i:s');
  exit;  
}

?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
  
  sendPoll();
  
  function sendPoll()
  {
    $.post( "poll.php", { uid: "uid" },
      function( data ) {
          $("#result").append("<p>" + data + "</p>");
          setTimeout(sendPoll, 5000);
      }
    );
  }
  
});

</script>

<div id="result">
</div>

There's alot to consider though.

How many people will be using this system? How accurate does it need to be? Is it for just internal use, or externally?

Like I mentioned, what i've suggested should probably only be used in a small scale situation, if you've got many people using it and it needs to be extremely precise, I wouldn't advise this kind of solution.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you auto-logout someone?

Post by simonmlewis »

It's for use at the moment by just a handful of people.
But hopefully by many more.
It's a tricky one. I am very tempted to put a script into a web site that uses this script, and if the time is after say 8pm, then run a script that marks the person "offline", but that too wouldn't fix theproblem if it is used on more sites.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply