Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Auto Logoff and Auto-direct to login page

Posted on 2007-03-27
4
Medium Priority
?
1,055 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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)
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 +…
Suggested Courses

618 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