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 14 of 14
  1. #1
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts

    Window Close Script

    im making an intranet site that requires login.

    how can i log users out when they close the browser??

    thanks
    Arctic.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    ps, i have this so far but it fires every time the page changes and not just when the window closes:

    Code:
    <script type="text/javascript">
    var needToConfirm = true;
    window.onbeforeunload = confirmExit;
    
    function confirmExit(){
    if (needToConfirm)
    	alert ("You need to log out before closing the browser");
    }
    </script>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Despite the similar sounding names, Java is not the same as Javascript.
    Moving from Java forum to Javascript forum.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is not possible with Javascript. You need to use server-side coding.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    any pointers?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    You have the right idea, but you need to go further.

    Start with this article:
    Code:
    http://www.4guysfromrolla.com/webtech/100604-1.shtml
    But then you *CAN* go even further.

    After doing that, then in your window.onunload event handler, you can send an AJAX request back to the server that logs the person off. Naturally, you would only do so if the needToConfirm flag (as shown in that article) is true.

    You can simply ignore the response from the AJAX request and assume that the logout worked. After all, there is at that point nothing you can do to prevent the movement to the other page, anyway.

    Philip is right that you must use server-side coding, but that's what the AJAX request will do: invoke your server-side code to do the logout.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    ok im glad i thought about this before i did most of this project :S

    i have this that seems to work fine

    if you can suggest anything different all your ideas are welcome


    in my "template.php" i have the following:

    Code:
        
    <script language="JavaScript">
      var needToConfirm = true;
    
      window.onbeforeunload = confirmExit;
      function confirmExit(){
        	if (needToConfirm == true){
          		if (window.XMLHttpRequest){xml1http=new XMLHttpRequest();}
    		else {xml1http=new ActiveXObject("Microsoft.XMLHTTP");}
    		xml1http.open("GET","logout.php",true);
    		xml1http.send();
      	}
      }
    </script>
    "logout.php" =

    PHP Code:
    <?
        
    include ("includes/session.php");
        
    $Update Q("UPDATE `user__users` SET `UserStatus` = 0 WHERE `user__users`.`UserID` = " $_SESSION['UserID']);
        
    session_destroy();
        
    header('Location: index.php');
    ?>

    in my "functions.php" i have:

    PHP Code:
    $SKIPIT 'onclick="needToConfirm = false;"'

    then on my menus i have each link
    PHP Code:
    <class="P1" '.$SKIPIT.' href="'.$menulink.'" style="height:18px;line-height:18px;"><span>'.$M1['MenuNumber'].' '.$M1['MenuName'].'</span></a
    and on each page modified all the forms as such:

    PHP Code:
    $Content .='
    <input type="submit" name="Action" value="Update" '
    .$SKIPIT.'/>
    // or
    <input name="Action" type="button" Value="Add New Customer" onclick="needToConfirm = false;window.location=\'211.php?ID=0\'"/>
    // or
    <select name="ID" id="ID" style="background-color:#66FF99" onchange="needToConfirm = false;window.location=\'211.php?ID=\'+(this.value)">
    //etc
    '

    keeping the user loged in while they move around the site
    but it logs them out if they close the tab or browser

    Thanks for all your help
    Last edited by Arcticwarrio; 08-02-2012 at 12:58 PM. Reason: added header redirect to logout.php incase a user manually clicks logout

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    The only thing wrong with this implementation is that it doesn't give a user a chance to say "Oh! I didn't MEAN to leave the site! Let me stay here and don't log me out."

    If you did as I suggested, and had the onbeforeunload do the conventional thing it was designed to dd, you let it *ASK* the user if he/she really intends to leave the site. If the user *cancels* the leaving, then he/she is still on the site, still logged in. So now you simply move your code that you have here shown as attached to onbeforeunload to onunload, instead!

    But there's nothing "wrong" with what you have. It just doesn't give the user a chance to cancel the leaving.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    like this?

    Code:
    <script language="JavaScript">
      var needToConfirm = true;
    
      window.onbeforeunload = confirmExit;
      function confirmExit(){
        	if (needToConfirm == true){
        	return "message to display in dialog box";
          		if (window.XMLHttpRequest){xml1http=new XMLHttpRequest();}
    		else {xml1http=new ActiveXObject("Microsoft.XMLHTTP");}
    		xml1http.open("GET","logout.php",true);
    		xml1http.send();
      	}
      }
    </script>
    EDIT

    just had a play around with the site and that seems better thanks again Supreme Master Coder
    Last edited by Arcticwarrio; 08-02-2012 at 10:07 PM.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    That can't possible be right.

    return *MEANS* what it says: "Return from this function RIGHT NOW! Do not even THINK about executing any code in this function past this return!".

    No, I meant:
    Code:
    <script language="JavaScript">
      var needToConfirm = true;
    
      window.onbeforeunload = confirmExit;
      function confirmExit(){
        	if (needToConfirm == true){
        	    return "message to display in dialog box";
      }
    
      window.onunload = function() {
        	if (needToConfirm == true){
          		if (window.XMLHttpRequest){xml1http=new XMLHttpRequest();}
    		else {xml1http=new ActiveXObject("Microsoft.XMLHTTP");}
    		xml1http.open("GET","logout.php",true);
    		xml1http.send();
      	}
      }
    </script>
    See? Two steps, not one.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    thanks, i'll try that in the morning

  • #12
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Old Pedant View Post
    That can't possible be right.

    return *MEANS* what it says: "Return from this function RIGHT NOW! Do not even THINK about executing any code in this function past this return!".

    No, I meant:
    Code:
    <script language="JavaScript">
      var needToConfirm = true;
    
      window.onbeforeunload = confirmExit;
      function confirmExit(){
        	if (needToConfirm == true){
        	    return "message to display in dialog box";
            }
      }
    
      window.onunload = function() {
        	if (needToConfirm == true){
          		if (window.XMLHttpRequest){xml1http=new XMLHttpRequest();}
    		else {xml1http=new ActiveXObject("Microsoft.XMLHTTP");}
    		xml1http.open("GET","logout.php",true);
    		xml1http.send();
      	}
      }
    </script>
    See? Two steps, not one.
    I now have this as you suggest but the xml is not firing
    Last edited by Arcticwarrio; 08-03-2012 at 08:29 AM.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Oh wonderful. It works in Firefox and MSIE 9 (and I'm 90% sure would work in MSIE 7 and 8), but...it does NOT work in Chrome!

    Here is the simple test I used:
    Code:
    <html>
    <body>
    good exit: <a href="junk0.html" onclick="needToConfirm=false; return true;">okay</a>
    <hr/>
    bad exit: <a href="junk0.html">oops</a>
    
    
    <script type="text/javascript">
      var needToConfirm = true;
    
      window.onbeforeunload = confirmExit;
      function confirmExit(){
        	if (needToConfirm == true){
        	    return "are you sure you want to do this?";
            }
      }
    
      window.onunload = function() {
        	if (needToConfirm)
            {
                alert("would log them off");
      	}
      }
    </script>
    </body>
    </html>
    Firefox ignores that string in the return "are you sure you want to do this?"; and only puts up its standard message, but it works.

    Chrome puts up the message for the return, but it does *NOT* do the alert("would log them off");

    Apparently, it doesn't allow *ANYTHING* in the window.onunload!

    *SIGH* Sorry!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    Looks like this is a "feature" of Chrome. Ugh. Okay, go back to doing it all in the onbeforeunload, I guess. Or do browser detection.

    There's a looonnng list of comments on the bugs in Chrome's onbeforeunload [which did NOT work at all in older versions of Chrome] and onunload, which still seems to be unfixed.

    *sigh*
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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