Page 1 of 1

Check if user is online

Posted: Wed Sep 05, 2007 4:24 pm
by prophy
Could it be made a script that check if a user connection is breaked (for example internet connection is stopped or user is closed the browser). And not only to be displayed error message to the user but this event to be registered by my PHP script.
For now the only idia that i have is a JavaScript event onClose() of the browser but this is not the solution that I want.
I was thinking if there is a method for PHP to listening for an event of breaked connection with the user browser or something similar.
Thanks in advance.

Posted: Wed Sep 05, 2007 5:09 pm
by Zoxive
Most of the time sites that show "Users Online" is usually within the last 5 minutes, they just keep a log when a user hits a page.

Then they simple select and count how many users have hit the website within the 5 or so minutes.

Code: Select all

Select count(id) as Online FROM `logins` WHERE `timestamp` > (`timestamp` - INTERVAL 5 MINUTE)
// EXAMPLE

Posted: Thu Sep 06, 2007 3:20 am
by CoderGoblin
PHP cannot listen to when the user "breaks connection" as the connection is broken after the PHP page is sent to the client (finishes). The common method is to assume a user as logged out if they do not access a page on the site for a certain amount of time. You may want to look at the tutorial XMLHttp tutorial (who's online example) for an example.

Posted: Thu Sep 06, 2007 10:29 pm
by Z3RO21
With the use of AJAX and timed request on the server you can get near perfect real time user information. You can also have javascript grab an image with a src pointing to an php generated image (src="image.php") the php image could update the users activeness within the system then use javascript to setTimeout to what ever time interval you want. I have used both methods they both work pretty well.

Thanks

Posted: Fri Sep 07, 2007 9:13 am
by prophy
I'll use AJAX but but the problem is if the connection is broken (supose the internet cable is unpluged or ISP having some problems). In that case the problem could be solve after 5 sec. or 1 hour (to me it is important to know about this in max 60 sec.) and I was hoping that there is something that I can use to notify PHP that there is a problem and the user is ofline for a while.
I could tell if the user is closed the browser but unpluged cable is not user dependent (and I can't use AJAX to conect to the server every 1 sec).

Re: Thanks

Posted: Fri Sep 07, 2007 10:17 am
by s.dot
prophy wrote:I'll use AJAX but but the problem is if the connection is broken (supose the internet cable is unpluged or ISP having some problems). In that case the problem could be solve after 5 sec. or 1 hour (to me it is important to know about this in max 60 sec.) and I was hoping that there is something that I can use to notify PHP that there is a problem and the user is ofline for a while.
I could tell if the user is closed the browser but unpluged cable is not user dependent (and I can't use AJAX to conect to the server every 1 sec).
The server would poll the information, not the client. Clients who are connected would see updated information. If the client isn't connected, it only makes sense they would see outdated information.