Log in

View Full Version : Current People Viewing Site



SChaput
01-19-2009, 06:22 PM
I have a php driven site, is there some way to list current people viewing the site, list their IP addresses, and maybe using those IP's give a general location? Is php the right way to do this or is another way my best bet.
Thanks.

stu2000
01-19-2009, 06:33 PM
hi SChaput,

I'd say the best way to do that would be to take the uesrs IP every time they log in/visit a page and record it along with their username and the time in a table in your database, then you can list all the people who have logged in/visited a page by calling the info from that table of all users that have logged in/visted pages within the last x minutes.

You can do this through standard PHP functions such as $_SERVER['REMOTE_ADDR'], which will give you the users IP.

Does that make sense?

Stu

SChaput
01-19-2009, 07:06 PM
I don't i even think i need to go that much into detail with it. The page that i want to monitor has no authorization on it and does not require the user to log in to anything. I guess i just simply need to find out how many different IP's are on the site. And print out, "5 People are Viewing:

jackbenimble4
01-21-2009, 10:30 PM
I don't i even think i need to go that much into detail with it. The page that i want to monitor has no authorization on it and does not require the user to log in to anything. I guess i just simply need to find out how many different IP's are on the site. And print out, "5 People are Viewing:

Actually, what stu suggested is the simplest solution possible.

It's important to understand that the server never knows how many people are viewing a page at any moment. All the server knows is a request to a particular page was made at some particular time. For all the server knows, the visitor immediately closed their browser. The link between the server and the client is ended once the page is loaded.

They way most sites show data about how many users are visiting a page is through sessions (http://us.php.net/session). The server can then keep track of a user as they browse the site. When a request hasn't been made in a certain amount of time, the user is considered to no longer be viewing the page.

The simplest solution would be to create a database table to track ip address's, the page, and the time viewed, and count the number of unique ip addresses within the past 20 minutes or so. For example, something like this for a query:


SELECT COUNT(log_id) FROM page_logs GROUP BY ip_address WHERE (load_time + 1200) > UNIX_TIMESTAMP() AND page = '...'

The only possible way to truly show the actual number of users who are viewing a page is through Javascript and Ajax, and an insane amount of HTTP requests. Basically, it's terribly impractical and likely way beyond the amount of work you want to put in for this.

Additionally, keep in mind that a new ip address does not guarantee a new user. Some ISP's, like AOL, actually assign a new address for every request. You could possibly end up with hundreds of false users if you rely on the ip address to determine unique users. It's a better idea to use sessions, either through cookies or a url parameter. Both are supported by PHP's sessions implementation.

npsari
01-22-2009, 12:11 AM
Yes, AJAX will be even better, because you can refresh the data often to update people on how many visitors are on the site

I really need to add this feature on my sites too, interesting post

jackbenimble4
01-22-2009, 12:35 AM
Yes, AJAX will be even better, because you can refresh the data often to update people on how many visitors are on the site

I really need to add this feature on my sites too, interesting post

Depending on the frequency you ping the server, using Ajax for this might be a waste of server resources. I would never implement something like this because I don't think my server could withstand the constant bombardment of requests. Additionally, you'll want to keep in mind that many people browse in multiple tabs or windows. When visiting a site, I usually begin by opening everything I'm interested in in separate tabs. Having HTTP requests coming in from each one of these pages could not only slow down the user's browser, but seriously confuse your system.