Link to home
Start Free TrialLog in
Avatar of gracesoft
gracesoft

asked on

Classic asp application - We need to track when user directly closes the page instead of logging out. This is needed to clear session values.

Hello Experts,

In our classic asp application which serves hotel industry, when a user logs out the application, we will clear the sessions so that other system users will not able to login with the URL without logging in with the credentials.

But when the user directly closes the pop-up window or the browser tab or when the page is reloaded/refreshed we are not able to track it to clear the session. Need help on this, please.

This is the code we use:



When we use window.onbeforeunload function


 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
     
         $("li a").click(function () {
            window.onbeforeunload = null;
        });
             $("a").click(function () {
            window.onbeforeunload = null;
        });
        $("a").not('#lnkLogOut').click(function () {
            window.onbeforeunload = null;
        });
        $(".btn").click(function () {
            window.onbeforeunload = null;
        });
    });
    function getxmlobject() 
    { var x = null; 
        try { x = new xmlhttprequest(); }
        catch (e) { try { x = new activexobject("msxml2.xmlhttp"); }
            catch (e) { x = new activexobject("microsoft.xmlhttp"); } } return x; 
    }

    window.onbeforeunload = function (e) {
        debugger;
        
        if (show_close_alert) {
     
            var xmlLoginvalues = GetXmlObject();

            if (xmlLoginvalues == null) { alert("Can't get Account code"); }
            var url = "../User/AjaxFunctions.asp?Mode=ClearSession";
            alert(url);
            xmlLoginvalues.open("GET", url, false);
            xmlLoginvalues.send(null);
            var LoginCredentials = xmlLoginvalues.responseText;
             window.location.href = "../logout.asp";
           // return false;
        }
        else
            window.location.href = TargetFile;

    }
   
    function unhook() {
        show_close_alert = false;
    }
    
</script>


Logout.asp page

<%@ Language=VBScript %>
<!--#include file="xxxx.asp" -->
<!--#include file="xxxx.asp" -->
<%
Call OpenConnection() 
pid = Session("PID")
WId   = Session("UID")    
ud     = Session("UName")
If Session("ViaMaster") <> "Yes" Then Call SaveLogDetails(ConnObj,Wid,session.SessionID,pud,uid,0,"PXX")

Session("ViaMaster") =""

  Response.Buffer = True
  Response.Expires = 0
  Response.CacheControl = "no-cache"
  Response.ExpiresAbsolute = now() -2

OnlineSystem = Session("OnlineSystem")
%>

<%
'### Clearing the session variables ###
session.Abandon()


strSQL2 = "delete from DATABASE2..Config where SessionID='"&Session("myGuid")&"'"
ConnObj.Execute(strSQL2)
Call CloseConnection() 
If Session("Distributor")="xxxs" then
    Response.Redirect "loginpage/XXXLogin.html"
else
   
    Response.Redirect "login.asp"
end if' redirecting  to login.asp
%>

Open in new window



Thanks,
Gracesoft
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

set a timeout on your session and have the pages reset the timeout.
I line 80 you have
strSQL2 = "delete from DATABASE2..Config where SessionID='"&Session("myGuid")&"'"

Open in new window

If this means you are tracking sessions in your database, then on a page refresh, why not check the database to see if the session exists?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.