?
Solved

Auto Logoff and Auto-direct to login page

Posted on 2007-03-27
4
Medium Priority
?
1,054 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
badrulnm earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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.
Suggested Courses

765 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