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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Update database on browser exit

    I need a bit of advice please...I am working on a chat program. When a user logs in, a value in the database is set to 1 (online), and when he/she logs out it is set to 0 (offline).
    The probelm is, if a user closes the browser, he/she is still online; not only this, but the session (variables; $_SESSION['username'] etc) is still active aswell.
    I was wondering if there is a way around this problem? I have tried onbodyunload in the body tags but cant get it to work. Somebody has suggested having a timer so it automatically logs out after a set time bu i have not got a clue how to do this. If anybody has any advice, please help me it is driving me mad...thanks you very much in advance for any replies

  • #2
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    Try an Ajax request to the DB on Js unload.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, thanks that was what I was hoping to do but i dont know how to go about doing it, do i put onunload in the html body tags, and point to a js file which points to a php file with the query? Thank you very much for your time

  • #4
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    I tried onunload with ajax and it does not work(the database is not populated) , i'll post later if I find another solution.
    Anyway your session should be destroyed if someone quits the browser, maybe your doing something wrong with sessions.
    Last edited by sybil6; 11-13-2010 at 12:31 AM.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <body onunload="doUnload()">
    ...
    function doUnload() {
      //ajax request to PHP file?
      alert("Updated database.\nSorry to see you go!");
    }
    Maybe that will work?

  • #6
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sybil6 View Post
    I tried onunload with ajax and it does not work(the database is not populated) , i'll post later if I find another solution.
    Anyway your session should be destroyed if someone quits the browser, maybe your doing something wrong with sessions.
    Thanks sybil6...if u find a solution it would be awesome if u let me know...

    Thanks also j9sjam3 i have tried something like that...it doesn't work unfortunately, if i have the js function in the header, it sets it to offline automatically, if its external file it does nothing atall thanks anyway...if i find a solution i will post it back aswell


  •  

    Posting Permissions

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