Solved

Auto Logoff and Auto-direct to login page

Posted on 2007-03-27
4
1,043 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now