Link to home
Start Free TrialLog in
Avatar of suhaiza
suhaiza

asked on

Auto Logoff and Auto-direct to login page

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.
ASKER CERTIFIED SOLUTION
Avatar of badrulnm
badrulnm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suhaiza
suhaiza

ASKER

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);

            }
Avatar of suhaiza

ASKER

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>
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;