Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Detecting and displaying multiple users

    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
    Code:
    guest 1
    a second person visits and once the AJAX updates both pages display:
    Code:
    guest 1
    guest 2
    so on and so forth, now if the first person leaves it shows:

    Code:
    guest 2
    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.

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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)
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah awesome, I didn't even know about the "onunload" event. This fixes my delimna, thanks greatly!

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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)

  • #5
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    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
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •