Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Logout help

  1. #1
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logout help

    Allright so i have a chatroom in which you can click "Logout" and you are then logged out and your name is taken off of the list of people currently in the chatroom. However, if you do not click Logout, and simply close the window, the session variable isn't destroyed and your name is not taken off the list because PHP doesn't do event based functions. At least thats what i think.

    So here's my question: Is there a way to, for example, call function Logout() in PHP when the window closes? Or should i use javascript like this:
    Code:
    <script language="javascript">
    document.onUnload = document.write('<?php Logout();?>');
    </script>
    Is it document.onUnload or window.onUnload? I just blanked on that hehe...

    Anyway, any help on this would be great.
    Thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That won't work at all, since the PHP is executed before the Javascript, so Logout() will be run every time the page is loaded It's window, but as with window.onload, I think document.onunload has the same effect (note the case).

    To do this, you must devise a fairly complex system whereby the last-seen time of each user is stored, and set a reasonable timelimit after which any page accesses (by anyone) will log that user out.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh boy.
    I think i've got myself into something way over my head. Could you show me an example Twey, please?

    Thanks.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    http://twey.co.uk/files/chat.phps

    It's an actual script I wrote for someone else, so it's a little more complex than it needs to be for an example, but I'm sure you can pick out the relevant bits.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Holy cow...how long did it take you to write that?

    Oh, and thanks.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't know, I just wrote it between other things, like everything I do on this forum.

    It's not that big
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Allright Twey, I've finally gotten to a point in my process of learning PHP & MySQL that i can read through your chat script that you provided, and find the necessary step to achieve what I'm going for here. I did read it and i now have the basic idea of what i'm doing, but theres one thing i wanted to ask you.

    Code:
    mysql_query("select * from $usertable where logged=1 and DATE_SUB(NOW(),INTERVAL 30 SECOND) > lastseen;");
    That query makes pretty much no sense to me, especially the (NOW(),INTERVAL 30 SECOND). I'm pretty sure this is the part where every thirty seconds it checks the user to see if they're still in the chatroom. But would you mind explaining this one query for me? And, what does DATE_SUB do?

    Thanks.

  8. #8
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anybody have an answer?

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    And, what does DATE_SUB do?
    DATE_SUB() is the key to this one It subtracts one date from another. INTERVAL 30 SECOND returns a DATETIME object to the value of 30 seconds.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OOOoooohhhhhhhhh.
    Its all starting to make sense now .
    I think i got this under control now.

    Thanks Twey.

Bookmarks

Posting Permissions

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