Check if user is online

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
prophy
Forum Newbie
Posts: 2
Joined: Wed Sep 05, 2007 4:13 pm

Check if user is online

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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.
prophy
Forum Newbie
Posts: 2
Joined: Wed Sep 05, 2007 4:13 pm

Thanks

Post 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).
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Thanks

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply