Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Track login and logout even if browser is closed

I would like to track when a user logs into my ASP website and when they logout, but most users either go to another website or simply close their browser so there is no "logout" happening.

My way to solve this is to use onunload= inside the <body> tag and then call a javascript function that writes a record to my table with the sessionID, username and timestamp.  The function would first check to see if a record already exists for that sessionID and if so, simply update the timestamp, if one doesn't exist, it would create a new record.

I think this would do what I am looking for but I do not know how to code writing to a DB from within Javascript.  I know how to do so in ASP, but I don't want to call another ASP page because my site consists of many pages and if a user goes from one page to another, that would trigger an onunload= on each of them, If I called a logout.asp page, that would keep my user from going to their intended page within my site no???  That is why I figure I need to write the record to the DB from within javascript.

If I can simply call another ASP page to write the record and the user would still go to the intended page, please let me know and I will go that route.  Thanks!
0
mgiusto
Asked:
mgiusto
1 Solution
 
SvenTech Lead Web-DevelopmentCommented:
What if the user has javascript deactivated?

Try to use global.asa and Session_OnEnd-Event!
0
 
mgiustoAuthor Commented:
It is a company intranet application and if the user has javascript disabled, the site won't work, but if you feel session_OnEnd is the way to go, I'd be interested if you could provide some code for me to work with.
0
 
kevp75Commented:
I always try to stay awa from using the global.asa file  (some old nightmares still haunt me...)

I would suggest using a javascript event onunload.

I use the following to fire a page onunload of a chat room.
<script language="javascript" type="text/javascript">
            <!--
            function makeRequest(url){
             if (window.XMLHttpRequest){
              http_request = new XMLHttpRequest();
             }
             else if (window.ActiveXObject){
              try {
               http_request = new ActiveXObject('Msxml2.XMLHTTP');
              } catch (e) {    
               try {
                  http_request = new ActiveXObject('Microsoft.XMLHTTP');
               } catch (e) {}
              }
             }
             http_request.onreadystatechange = function() {
              if(http_request.readyState == 4){
               if(http_request.status != 200){
                  if(http_request.status == 403) alert('Forbidden');
                  else if(http_request.status == 404) alert('Could not find file.');
                  else alert('Unknown Error');
               }
               else {
                  try{http_request.setRequestHeader('content-type','text/html;charset=ISO_8859-1:1987');}catch(e){}
                  if(http_request.responseText.substring(0, 5).toLowerCase() == '<?xml') alert('This is Valid XML');
                  else findTxtLinks(http_request.responseText);
               }
              }
             };
             http_request.open('GET', url, true);
             http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
             http_request.send(null);
            }              
            function RorC() {
                  var top=self.screenTop;
                   if (top>9000)
                   {makeRequest('/leavethesite.asp?usersID<%=passTheUSersIDInThisQuerystring%>')}
            }
            //-->
            </script>
            <body onUnload="RorC();makeRequest('/leavethesite.asp?usersID<%=passTheUSersIDInThisQuerystring%>');">


and then make a page called leavethesite.asp that will delete their session variable/cookie  (or if you do this via db, mark their record as logged out...)

HTAH
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now