Re: close the browser
| From: | Stuart Dallas | Date: | Tue, 24 Jun 2014 17:30:21 +0000 |
| Subject: | Re: close the browser | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323536@lists.php.net to get a copy of this message | ||
On 24 June 2014 18:18, hadi <almarzuki2011@hotmail.com> wrote:
> >I get that, but why do you want to run a script? Based on your previous
> >emails it looks like you're trying to manually expire a session. Sessions
> >usually expire due to a timeout since the last request, and I've never
> >come
> >across a reason to do it any other way. So again, what is it that you are
> >actually trying to achieve?
>
> >If you want to apply a more complicated set of session expiry rules you
> can
> >do so with a custom session handler, but I would question the need for
> such a
> >thing.
>
> Well im trying to achieve one thing here. If a user is logged in and if he
> try to logged in again from different pc with the same username/password it
> well flag him out. Because he already logged in.
> About connection close, when the user close the connection it well clear
> the
> flag.
> So what do I use to accomplish this ?
I would start with a custom session handler. Possibly something similar to
this: http://3ft9.com/mysql-sessions/
Modify the methods that write the data to MySQL so it first extracts the
username and stores that in a separate field so you can do lookups on that.
Set the lifetime to something relatively short (say 60 seconds), then have
a webpage that the user must keep open that is making a request to the
server at roughly half that interval (e.g. every 30 seconds).
Modify the Session::gc method so it does what's necessary to cleanup the
network config when a session expires.
Add a method so your login system can query the session store for existing
users with that username, or alternatively modify the Session::write method
to refuse to store a session if there's already a session for that username.
Using a timeout rather than trying to detect when the user closes their
browser is preferable primarily due to the unreliability of that detection.
A number of browsers have rules around the use of the onunload event that
may prevent you from seeing a particular user disconnect.
Personally I would be looking in to implementing this at the network level
rather than PHP. The network must be able to detect when a device drops off
the network, and that would probably be a better place to implement the
cleanup.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/