View Full Version : global.as error
ahmedsoliman
05-07-2003, 04:51 PM
now i redesigning asp chat application,and i need when user shutdown IE without loging of, its status change in my db.
so i used this code but it dosen't work.
global.asa File
<script language="vbscript" runat="server">
varUsername = session.contents("Username")
varDate = now()
sub Session_onEnd
varSQL="UPDATE USERS SET RoomID=0, [Time]='" & varDate & "', Active=0 WHERE Username = '" & varUsername & "' "
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open varDB
conn.Execute(varSQL)
conn.close
Set conn = Nothing
end sub
</script>
Roy Sinclair
05-07-2003, 05:03 PM
Try this:
<script language="vbscript" runat="server">
sub Session_onEnd
varUsername = session.contents("Username")
varDate = now()
varSQL="UPDATE USERS SET RoomID=0, [Time]='" & varDate & "', Active=0 WHERE Username = '" & varUsername & "' "
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open varDB
conn.Execute(varSQL)
conn.close
Set conn = Nothing
end sub
</script>
Don't expect global variables set up when the "application" (not a user's session) starts to still be available when a user's session ends. For a long running "application" there could literally be years between those two events.
now i redesigning asp chat application,and i need when user shutdown IE without loging of, its status change in my db.
If a user closes his browser, there is no server interaction and the session stays allive. So the session_onend sub isn't called.
The session will stay alive until it times out = 15 minutes after the last request (if you use the standard timout setting)
ahmedsoliman
05-08-2003, 08:31 AM
then how to get this action(shutting down browser) and berform the same process
remember that with asp, you don't have a persistent connection between the client and the server !
there are a few options to simulate some sort of connected-checking, but they all create some load on the webserver.
You could set the timout lower. Like
Session.Timeout = 1
This will end all sessions if the user hasn't sent a new request (= requested a page) within 1 minute. And the session_onend sub will be called then. But of coarse, this will create some serverload + will end session that should have been kept alive !!
You could combine this with some javascript that reloads a page after 1 minute.(for instance, have a framepage, with a very small frame (1% or so, without border or scrollbars)with some javascript to reload the page 58 seconds after onload)
So if you'de set the timout to 1 and reload the page each (minute - few seconds), then you'd have a quite accurate view on the sessions that are still alive (but there will be still a delay of max 1 minute).
But this creates some traffic + extra serverload, so you should only use it if it's really important to you and if you don't have to many clients on that server!!
Maybe there is another way. Maybe there exists some javascript that sends a request to the server if a window is closed ? But even then. If the user just goes to an external url, you'de still mis him.
Or maybe there is a way to ping a client (never heard of it, but it's probably not completely impossible).
Or you could open your site in a fulscreenwindow without bars (and disable the alt + F4 and the windows-key and who knows whatever other possible keycombination --> my keyboard has allsorts of extra programmable keys) to force users to use the logout-link. But a lot of surfers wount like that and it gets mighty complicated for maybe a fairly unimportant feature like "current users online" ...
And a client could still just turn of his machine or shut turns his modem of.
Roy Sinclair
05-08-2003, 03:25 PM
You still get the session end regardless of how the user leaves your site, it just happens to occur at then end of the session timeout. More important, you should make sure your application start code (as opposed to session start code) clears out any remaining "user is logged on" entries in any database you are using, that way you don't end up with users marked as logged on if something goes wrong and your application is restarted before all users are marked as logged out.
ahmedsoliman
05-11-2003, 08:46 AM
thanks for all and thanks for raf specially, but this problem is common as i think and needed in many asp application.
for example shopping cart, if some one select some products and put them in the cart and he suddenly shut down the browser, i think his data should be removed from db with same consept.
now i have few last questions about this:
if he shutdown his browser and seesion timout is 15 min ,will server response after 15 min and perform the process in case he still connected to internet?
is there a way to use array instead of db to get over this prob?
has asp.net new solution for this problem?
but this problem is common as i think and needed in many asp application
Well, it's not an ASP problem. It's basically an HTTP problem. All webtrafic that uses this protocol, doesn't have a persistent connection. And that is not necessarely a problem. You just need to keep your db clean. There are several options for that (see below).
Doen't know asp.net enough but i don't think it is fundamently different since you still use HTTP.
Now, about keeping the db-clean.
Option 1 : cookie
+ --> not a lott of load on your server. user can end a session and come back later and continue the session he broke of (if you specify an expirationdate when you set the cookie). This requires some analysing though, since you need to be able to start the ordering process where he stopped !!
- --> not all users accept cookies. on shared computers, this could lead to erors and privacy issues
Option 2 : session variable of type array
+ --> no acceptance needed from user. Fast. Safe
- --> if you have a lot of simultanioues users, this might take up some resources ... If the session times out, the instance of the session-object for this client is destroyed. even if he left his PC for a while and want's to continue later.
Option 3 : db; temporarely table
You can store all the info in a temporarely table. (be sure to register the date and time!) If the user confirms his order, you can then transfer that info into the actual orders table(s). If the session is terminated (before confirming), you can retrieve this info on his next visit. This requires some analysing though, since you need to be able to start the ordering process where he stopped !!
If you want, you can periodically clean out this table (manually, or with a manually activated script or automatically with a CRON job.
db;cofirmvariable. Another option is to store all info directly into the actual tables, but include a 'confirmed'-variable, that is set to 0 by default, and is updated when the user confims.
You could also try to combine 2 and 3, where you save the sessionvariables info to the db before destroying the session. I believe the session is destroyed right after performing the session on end sub. (I know for sure it works like that when you use session.abandon, so i presue it is the same on timout)
if he shutdown his browser and seesion timout is 15 min ,will server response after 15 min and perform the process in case he still connected to internet?
If a client doesn't send a new request to the server (= if he doesn't refresh a page or hits a link or there's no javascript that request a page or whatever) in the timout period (20 minutes=standard), then the session is ended. It makes no difference if he is connected to internet, has his browser open, moved to another site or has just fallen asleep. There's no client -server interaction needed for ending and destroying te session.
ahmedsoliman
05-11-2003, 01:48 PM
then what is the penefit of session_onend function, which is located at global.asa file, when no client -sever interaction?
as i understand from you this function useless, unless it will perform some thing at client side
then what is the penefit of session_onend function, which is located at global.asa file, when no client -sever interaction?
The benefit? Well, you can specify which action need to be performed when the session ends (when you abandon the session after a user hits a logout link or after the session times out). You can write something to the log, the db, change an applicationvariable, ...
When there is no client interaction, the session needs to be destroyed, cause the sessionmanagement takes up some serverresources. If these sessions would not be destroyed after some time, it would take unnescesary resources + create a securityrisk (--> sessions would stay alive and someone else could work with the app hours after you logged in)
So the benefits are that it gives you the option to perform some action if the client leaves the site (or has left it some time ago) + free resource and control the applicationvariabels. So it certainly isn't useless.
Don't think there happens anything on the clientside.
whammy
05-12-2003, 03:49 AM
I would definitely check out some books on server-side ASP, like Beginning ASP 3.0 by Wrox.com... I hate to keep recommending this book, but I knew all of this stuff once I read it.
Don't forget to check out http://www.w3schools.com !
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.