Solved

Auto Logoff and Auto-direct to login page

Posted on 2007-03-27
4
1,046 Views
Last Modified: 2013-12-08
I already do a search here but still i didn't get the right code even i tried to modify few of the codes that maybe similar to my problem.

My problem is, i want to auto-logoff user whenever they close the browser by clicking the red 'X' button up there.Like when i login into Yahoo Mail, and then i click the 'X' button to close the browser without logout.When i try to open my yahoo mail again, it will direct me to login page, asking me to login again before i can read my mail.

I'm using asp.net. I try to add

<body  onunload="onClose()">

and then before <head> , i put

<script type="text/javascript" language="javascript">

      char id = Request.Form("UserIdGWCAdmin");
      char name = Request.Form("UserNameGWCAdmin");

      document.cookies = "userID = id";
      document.cookies = "userName = name";
      
                      function onClose() {
                             var iX = window.document.body.offsetWidth - window.event.clientX ;
              var iY = window.event.clientY ;

            if (iX <=30 && iY < 0 )
            {
                 var Yesterday=new Date();
                 Yesterday.setDate(Yesterday.getDate() - 1);
                  myExpire = (Yesterday.getMonth() - 1) + "/" +  Yesterday.getDate() ;
                  myExpire += "/" + Yesterday.getFullYear();
                  document.cookies = "userID="+id+"; expires="+myExpire.toGMTString();
                document.cookies = "userName="+name+"; expires="+myExpire.toGMTString();

                            }
      }

</script>

but still, when i open again the browser, it will again go back to the page without asking me to login again.
0
Comment
Question by:suhaiza
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
badrulnm earned 500 total points
ID: 18805524
I'm not sure why do you need to test for " if (iX <=30 && iY < 0 )" in onClose() function. Will it work if you remove the if test?

Change:
function onClose() {
                             var iX = window.document.body.offsetWidth - window.event.clientX ;
              var iY = window.event.clientY ;

            if (iX <=30 && iY < 0 )
            {
                 var Yesterday=new Date();
                 Yesterday.setDate(Yesterday.getDate() - 1);
                  myExpire = (Yesterday.getMonth() - 1) + "/" +  Yesterday.getDate() ;
                  myExpire += "/" + Yesterday.getFullYear();
                  document.cookies = "userID="+id+"; expires="+myExpire.toGMTString();
                document.cookies = "userName="+name+"; expires="+myExpire.toGMTString();

                            }
      }

To:
function onClose() {
      var Yesterday=new Date();
      Yesterday.setDate(Yesterday.getDate() - 1);
      myExpire = (Yesterday.getMonth() - 1) + "/" +  Yesterday.getDate() ;
      myExpire += "/" + Yesterday.getFullYear();
      document.cookies = "userID="+id+"; expires="+myExpire.toGMTString();
      document.cookies = "userName="+name+"; expires="+myExpire.toGMTString();
}
0
 

Author Comment

by:suhaiza
ID: 18805582
I try to remove the IF statement, but still it didn't expired the cookies.I change my code a bit to this,

        function onClose() {
                  var iX = window.document.body.offsetWidth - window.event.clientX ;
                  var iY = window.event.clientY ;

                        alert(document.cookie);
                        var Yesterday=new Date();
                        Yesterday.setDate(Yesterday.getDate() - 1);
                        myExpire = (Yesterday.getMonth() - 1) + "/" + Yesterday.getDate() ;
                        myExpire += "/" + Yesterday.getFullYear();
                        document.cookies = "expires="+myExpire.toGMTString();
                        alert(document.cookie);

            }
0
 

Author Comment

by:suhaiza
ID: 18806153
I've change the code a bit, and manage to do a pop-up msg, showing the latest cookies. I got the cookies with expired date that i put 2 days back,but why did the page still available to view without needing user to login again.

<script type="text/javascript" language="javascript">
      
        function onClose() {
                  var iX = window.document.body.offsetWidth - window.event.clientX ;
                  var iY = window.event.clientY ;
                  var cookie = document.cookie

                        alert('cookie------->'+cookie);
                        var today = new Date();
                        alert('today------->'+today);
                        today.setTime( today.getTime());
                        expires = 1000 * 60 * 60 * 48;
                        var expires_date = new Date( today.getTime() - (expires) );
                        alert('expires_date------->'+expires_date);
                        expires = expires_date
                        alert(expires);
                        document.cookie = "expires="+expires;
                        alert('document.cookie------->'+document.cookie);

            }

</script>
0
 
LVL 5

Expert Comment

by:badrulnm
ID: 18806222
Could this be the problem?

      document.cookies = "userID = id";
      document.cookies = "userName = name";

Change the above to:

      document.cookies = "userID = "+id;
      document.cookies = "userName = "+name;
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question