Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2003, 01:01 PM
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default session_OnEnd not firing!

session_OnEnd doesnt fire =/
Here is my global.asa file:
Code:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
'Session("StatusId") is the status the user is, guest, member, admin, etc
'Session("UserId") is the user's unique id (unless he/she is guest then their user id is = 0)
Sub Application_OnStart
    Application("hitCounter") = 0
    'if Request.ServerVariables("SERVER_NAME") = "lucky" then
        Application("localPath") = "c:\Inetpub\wwwroot\"
    'else
        'Application("localPath") = "E:\path\"
            'end if
    Application("connString") = "File name=C:\temp\DataBaseDev.udl"
    'Application("connString") = "File name=E:\path\DataBaseProd.udl"    
End Sub
Sub Session_OnStart
    Session("UserId") = 0    
    Session("StatusId") = g

    session.timeout = 600
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'.txt file visitor counter
'////////////////////////////////
   Dim fso, f, ts,icounter,scounter
   Set fso = CreateObject("Scripting.FileSystemObject")
 'Set f = fso.GetFile("E:\path\db\counter.txt")
  Set f = fso.GetFile("C:\temp\counter.txt")
   Set ts = f.OpenAsTextStream(ForReading, TristateFalse)
   scounter = ts.read(7)
   ts.Close
   icounter = cInt(scounter) + 1
   scounter = cstr(icounter)
   Set ts = f.OpenAsTextStream(ForWriting, TristateFalse)
   ts.Write scounter   
   ts.Close
    Application("hitCounter") = scounter

End Sub
'///////////

'here is where i need help!!
'/////////////////////////////////////////////////////////////////
Sub Session_OnEnd    

dim sql
sql = "Update databaseTable set Last_Visit = Now() where UserID =" & Session("UserId")
conn.execute sql
sql = "Update databaseTable set userOnline=0 where UserID =" & Session("UserId")
'Response.Write sql
'Response.end
conn.execute sql
    Session("UserId") = 0
    Session("StatusId") = g

End Sub
'//////////////////////////////////////////////////////////////
</SCRIPT>
The bottom part is where i need help.
Is it possible to run a database query when the user CLOSES the window. Or at least delay the close until database query is run?
If so, how
I am using Access 2000, mind..
tnx

wisdom is knowing what to do next;
sanity is doing it...
 
Old December 22nd, 2003, 07:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can use the onunload event of the <body> tag to execute some JavaScript. For example:
Code:
<html>
<head>
  <title>Unload Example</title>
</head>
<body onunload="JavaScript:alert('Hallo');">
  <h1>Unload Example></h1>
</body>
</html>
This will pop up an alert box when you close the browser.
Instead of alert() you can open a new window that requests Logout.asp for example.

You should be aware of the limitations of this solution. First of all, it may still not work every time. Secondly, this can be a pain for dial-up users (so may not apply in an Intranet environment). Suppose someone with a dial-up connection browses to your site, requests a long page and then disconnects to read the page without being on-line. As soon as they close the browser, the popup will appear and they are asked to dial-in again. Not very user friendly.

Bottom line: you can't really be sure that Session_OnEnd will fire under every circumstance, so you should work around it. One thing I once did in a distributed Intranet (with dial-up connections), was to "logout" on EVERY page. We had an include file on every important page in our site that called Updatedatabase(UserID). This method would update the database to register every last hit a user made. This way, we could see at what time the user requested the last page. An "old" last date would indicate logout.

Not a perfect solution because it required some database overhead, but it worked for us.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 2nd, 2004, 02:33 AM
Registered User
 
Join Date: Jan 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to malinda Send a message via Yahoo to malinda
Default

Hi...

what u have written in Session_OnEnd is perfectly ok u can use it like that. but Session_OnEnd fires when the session ends not when u close the borwser window.

Session ends after the specified last interation time (set in IIS > site properties > Directory > Configurations > app object - Session timeout) with the server. That means Session_OnEnd fires after a set time when the browser close.

But u can make both happen at once u can call a intermediate ASP page when ur are loging out or closing the page. and in this page u can
code like this

<%

Session.Abandon

%>

this will force the session to end and will fire Session_OnEnd





Malinda
 
Old January 29th, 2005, 11:18 PM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How would you do that?

 
Old January 30th, 2005, 06:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Do what, exactly?

Some suggestions have made to call a page like Logout.asp on the onunload event. Is that what you're after? What exactly are you having problems with?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 19th, 2005, 04:01 AM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to WEBMASTERSUPER
Default

hi

   This is my first post, i have faced the same problem. i had found one solution. it is working perfectly. why don't you try this

          contents of ActiveX.Exe

                place all the database codes to reset login in activex.exe project then use it in your asp application


          solution steps :-

                1) create one ActiveX.EXE in vb.
                2) create a object for ActiveX.exe in global.asa
                3) Use it on session_onend event

With Regards

WEB Master Super





 
Old February 19th, 2005, 09:29 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Geeneeg
Offcourse you can explictly call session on End ,when client browser is closed.
Since it's not a good idea to let the server to call it after 20 mints(default session timeout)
This code can help you
http://p2p.wrox.com/topic.asp?TOPIC_ID=1774

Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
Session_OnEnd Question.... seananderson Classic ASP Basics 3 July 19th, 2007 03:54 PM
session_onend and databases scottlet Classic ASP Databases 2 May 30th, 2007 04:47 AM
help with session_onend chinedu Classic ASP Basics 8 October 26th, 2004 10:13 PM
Session_OnEnd v. SQL bluewater68 Classic ASP Professional 0 August 18th, 2003 04:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.