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!
mgiustoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.