Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recording how a user logged off 1

Status
Not open for further replies.

Floodster

Technical User
Jan 28, 2005
204
I have read numerous threads about this & the best way is to use the GLOBAL.asa file. I can record in a database how a user logs off if they choose my log out option but the problem I have is when they just close the browser.

I believe you can use Session_OnEnd but I'm unsure if I can call a function from my gbl_function.asp file or if I need to include everything in the global.asa file.

The other problem is if I have to include everything in my global.asa file how do I use the session variables I have created in other .asp pages to store user_id etc in my SQL statement??

Thanks.
 
I've had a quick go at this but I need to wait now until my session times out, but here is the content of my global.asa.

<script language=VBScript runat=server>
Sub Session_OnEnd

set objConn=Server.CreateObject("ADODB.Connection")
sConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""c:\inetpub\ objConn.Open sConnect
sUser_Logname=Session("sess_User_logname")
sUser_ID=Session("sess_User_ID")
sUser_Logout=Now()
'lets stamp the date into the database when the user logged out.

ssSql="UPDATE tbl_LOGIN SET "
ssSql=ssSql & "user_id=" & sUser_ID & ","
ssSql=ssSql & "user_logout=#" & sUser_Logout & "#"

ssSql=ssSql & " WHERE user_id = "& sUser_ID & " AND user_logout Is Null;"
application.lock
objConn.Execute(ssSql)
application.unlock
objConn.close
set objConn=Nothing


End Sub

</script>

Can anybody spot any glaring errors???

Thanks.
 
Floodster said:
The other problem is if I have to include everything in my global.asa file how do I use the session variables I have created in other .asp pages to store user_id etc in my SQL statement??

in global.asa page you will have to declare your variable something like this...

Application("myvar")="blah"

and in other asp pages you can do...

response.write Application("myvar")

is that what you were asking...

-DNG
 
Not quite,
In my login page I store a few session variables for ID, Username & User Level.
I need to incorporate these into the SQL within the global.asa to update the database if the session time outs.

I hope I'm making myself clear!?!?!
 
not completely sure of your question but how about this:

sub Session_OnEnd
Response.Redirect("finalsql.asp")
end sub


and in the finalsql.asp page...do your sql query and either show the user any message or just close the window...

-DNG
 
but if we reach session end because the browser closed 20 mins ago then sending a redirect wouldnt work.
 
Also Session_OnEnd is not a function that you call... it is an Event function that is called by IIS when IIS closes the session... either by timeout or explicitly by Session.Abandon.

It is your last chance to do something with the session before it is destroyed.
 
Floodster,

Have you tried the code in your second example ? That should generally work (syntax errrors and similar permitting).
Also you create a variable sUser_logname, but never use it ?

You should probably remove your custom script used when the user manually logs out, and just use this one, as there *should* always be a session end event as Sheco mentions.

As the Session_OnEnd event is often kicked off after the specified timeout period for a session, you might want to subtract that timeout number (usually 20 mins I think) from Now() so that you have the real time the user stopped using the application. To ensure this doesn't conflict with Session.Abandon, you might want to add a flag / value in the session that you can use to conditionally subtract the timeout period (if it's appropriate).

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Cheers Guys,
This is all really useful information. Can I confirm that if a user closes the browser that would kick in the Session.Abandon or is that something else.
I've now got my code to update if a user is logged out by Session_On_End.
Thanks.
 

Session.Abandon is explicitly executed by your script. The 'same actions' occur after the session timeout automatically kicks-in, so effectively session.abandon removes the wait. But if the user closes the browser the timeout will kick in, not session.abandon.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
The server won't know the differnece between the user closing the browser, suffering a power outage, or simply being distracted by something else... they are all timeouts.

PS: I once had angry users for an interal HR site where users could post their career ambitions... some of them would type for so long into the textarea that they would time out... they hit 'submit' after 30 mins of typing ... only to be redirected to the login page. Ouch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top