Results 1 to 9 of 9

Thread: onbeforeunload => logout if closed? Working in IE only!

  1. #1
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Question onbeforeunload => logout if closed? Working in IE only!

    Had a question regarding logging out users who don't click Logout, who instead just close the browser.

    I have two scripts pretty much the same but they only work in IE due to that fact that onbeforeunload was implemented for IE.

    The latest version;
    PHP Code:
    window.onbeforeunload confirmExit;
    var 
    aClick=false;  
    function 
    confirmExit(e){  
    if(
    document.all)event;  
    if(!
    e)e=window.event;  
    if(
    e){  
    if(
    aClick==false && (e.target==document || e.clientX<|| e.clientY<0)) {  

    //alert("Logging out...");   
    // Add server call to logout the user

    /* Original code commented out because of page refresh, page reloads or form submits.

    alert('about to open the window');
    ajaxCall("http://example.com/logout.php");
    */
    //adding this allows for page refresh, page reloads or form submits in IE only
    function open_win() {
    ajaxCall("http://example.com/logout.php");
    }
    open_win()

    }
    }
    }
    function 
    ajaxCall(dname){
    var 
    xmlDoc;
    if (
    window.XMLHttpRequest){
    xmlDoc = new window.XMLHttpRequest();
    xmlDoc.open("GET"dnamefalse);
    xmlDoc.send("");
    //alert(xmlDoc.responseText); //Hide blank alert boxes.
    //return xmlDoc.responseXML; //Comment out - not needed.
    // IE 5 and IE 6
    }else if (ActiveXObject("Microsoft.XMLDOM")){
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async false;
    xmlDoc.load(dname);
    //alert(xmlDoc.responseText); //Hide blank alert boxes.
    //return xmlDoc; //Comment out - not needed.
    }
    //alert("Error loading document"); //Hide blank alert boxes.
    return null;

    Is there any method or way to get this to work in Firefox?
    If Yes, how?
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

  2. #2
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Default

    I've searched and not once has this question been answered along with any kind of code that works.
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

  3. #3
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    why not just use php session cookies instead?

  4. The Following User Says Thank You to thenajsays For This Useful Post:

    student101 (08-26-2009)

  5. #4
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Default

    Can it logout the user if they haven't clicked the logout, who instead closed the window?

    Edit: On my search for this method, haven't seen one example?

    Searching for php session cookies brings up different methods of doing this.
    Will explore...
    Thank you!
    Last edited by student101; 08-26-2009 at 09:05 PM.
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

  6. #5
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    it does it automatically i believe... im still learning php, but theres A LOT you can do... let me look and see how i did it and ill get back to you

  7. #6
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    ok, first things first, every page that you want the session to be vaild for, you have to have:
    PHP Code:
    <?php
    session_start
    ();
    ?>
    BEFORE ANY HTML
    then, you need to have a little snippet of code like this somewhere to check if the session has been initialized, like this:
    PHP Code:
    <?php
    if (!isset($_SESSION['cookieName']))
    {
        
    /*Do something*/
    } else
    {
        
    /*Do something else*/
    }
    ?>
    and finally, in your log in validation, if the username and password match the data base, you have to initialize the cookie:
    PHP Code:
       $_SESSION['cookieName'] = 'sessionID'
    the session ID can be a variable (such as the username)
    oh... one more thing... on the log out page you need to destroy the session, like so:
    PHP Code:
    <?php
    session_start
    ();
    session_destroy();
    ?>
    again, that has to go BEFORE any HTML

  8. The Following User Says Thank You to thenajsays For This Useful Post:

    student101 (08-26-2009)

  9. #7
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Default

    Good one, you got the jump on me.
    Cool let me play, read and post.

    Luckily you brought this up.
    Thanks again thenajsays!
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

  10. #8
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    dont forget to click the thanks button for me! and let me know how it turns out!

  11. #9
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Default

    Did that, just woke up.
    Off to code!

    Cheers
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

Tags for this Thread

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
  •