View Full Version : Detecting and displaying multiple users
04-12-2008, 12:26 AM
Does anyone have a strategy for displaying the people who are browsing the page?
Here's an example:
One person visits the page, it then displays
a second person visits and once the AJAX updates both pages display:
so on and so forth, now if the first person leaves it shows:
I was thinking maybe they could push a subit button or something that would add their name, but I just don't know how to make it so that it can detect that they're navigated away from the page.
04-12-2008, 03:02 AM
add an onunload to the body of your page that makes an ajax call to a page that removes their session from the list of active ones (implying that you've set up a session independent list of "active" sessions that you're getting the list of users from). have another page accept ajax requests that add their name to the list of currently active users, and another to return the list of currently active users, granted you could do all three in one, but you'll get better seperation this way & less hammering on a single file. these should all be fairly straight forward, there's no real data manipulation going on, just reading the value of a text box for their name, post the request to the page required using ajax, or make a post (or get) request to the page that returns the list of active users, and assign the innerHTML property of the element you want to display the list of users in to the responseText property of the returned xhr object, and thirdly (you can do this with a popup window even, no ajax required) request the "logout" page with the user's session id or user name in the query string. set the refresh on an interval of a few seconds & you're all set, no buttons required (for the refresh anyways, you can set the onclick of a button to make the request to submit the name of the user)
04-12-2008, 04:56 AM
Ah awesome, I didn't even know about the "onunload" event. This fixes my delimna, thanks greatly!
04-12-2008, 08:37 PM
I have one more question about PHP, how do I write to a file without erasing everything in it when it is opened?
For my first example I just want the PHP to be called, then it will add the name onto the list. Later when the person leaves it will find the name it wrote and erase it.
Also I haven't tried the onunload event yet but how can it run code if someone closes their browser? Since the browser's closed you'd think that it wouldn't have time to run whatever function you set it to? (such as an AJAX request)
04-13-2008, 02:03 AM
it would fire off the ajax request before the browser closed, thus as long as the packet is received by the server, the server side code would be allowed to operate as it is not dependent on the client in any way. the most obvious solution to keeping the existing contents of a file and adding a new entry would be to read the existing contents of the file into a variable, add the content you want to the variable and write the whole variable back to the file. the better solution would be to just open the file for appending instead of writing
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.