PDA

View Full Version : Sub Session_OnEnd - Update DB.


Morgoth
11-28-2002, 02:29 PM
This is in my Global.asa File.

Sub Session_OnEnd
Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";"
oConn.open StrConn

sSQL = "UPDATE Members SET Loggedin=False WHERE (ID=" & Session("ID") & ")"
Set oRS = oConn.Execute(sSQL)
End Sub

If there isn't something wrong with my Code then I don't know why it's messing up.

When a person leaves the server I want to take their ID number (if they had one assigned) and I want to log them out of the db. The problem is, it's not changing it back to false when a person leaves. Not even if I wait to find out after I slept on it. The name is still there, telling me that "loggedin" is still set to True.


Anyhelp on this?
I have not see a tutorial on this exact method, maybe there is another I am not seeing here?

BigDaddy
11-29-2002, 12:34 AM
Have you tried writing out the session id to the page, then taking that and doing a manual update query to the datebase with it to see what it turns up?

Morgoth
11-29-2002, 03:06 AM
So your telling me to test my code first?
Good idea, I don't think I did, I think I was too conceded, I own the ASP.

I will test my code, thank you Big.

:(

Morgoth
11-29-2002, 03:25 AM
Apperently my code is:

PERFECTLY FINE!


So I am not sure what my problem is. It has something to do with my Session Time out or something??

Here is my Global.asa As Is, EXACTLY! (I left the comments in cause I want to learn why and how still)


<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Dim oConn, StrConn, sSQL, oRS

Sub Application_OnStart
Application("ActiveUsers") = 0
End Sub

Sub Session_OnStart
' Change Session Timeout to 20 minutes (if you need to)
Session.Timeout = 20
' Set a Session Start Time
' This is only important to assure we start a session
Session("Start") = Now
' Increase the active visitors count when we start the session
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers") + 1
Application.UnLock
End Sub

Sub Session_OnEnd
' Decrease the active visitors count when the session ends.
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers") - 1
Application.UnLock

Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb") & ";"
oConn.open StrConn

sSQL = "UPDATE Members SET Loggedin=False WHERE (ID=" & Session("ID") & ")"
Set oRS = oConn.Execute(sSQL)
End Sub

</SCRIPT>

BigDaddy
11-29-2002, 05:13 AM
Sorry, dude. It was just a thought....

I know I've personally sat there staring at code for awhile, then find it's something silly, like the value that I'm looking for not actually being in the database.

Morgoth
11-29-2002, 05:14 AM
I hear that!
So does whammy.

whammy
11-29-2002, 05:24 AM
I'm not so sure about that. ;)

Have you made sure that the database code you're using works when you aren't using it in the global.asa file? ;)

If not, make a test page with the same script in it and see for yourself how it works by using Response.Write to write out the values for you. Since you're hosting, I'm fairly sure you have SQL Server installed, so you should be able to go to Tools and use Query Analyzer to check the syntax of your SQL statements, as well.

I have done some pretty silly stuff (as I'm sure everyone has!), but I am pretty darn good at troubleshooting (thanks in part to the silly stuff ;)), even if it takes me a little while to think about it at times. :)

I think BigDaddy may perhaps be able to attest to my troubleshooting abilities. :D

No offense, but you don't "own the ASP". I don't either, since I've only been using it for less than a year and a half... Glenn (glenngv) has a lot more experience than I do with programming, and I know some other developers that are extremely knowledgable as well, but I'm catching up as fast as the old brain can go. ;)

Morgoth
11-29-2002, 05:58 AM
Well, my code does work, the only problem is I am unable to find out if the Session ever closes in the first place.

My code can sit in there and almost never be used because the session ID number is set back to null or something.

I have that "Application("ActiveUsers")" application running, and that works ok or else that number would be going over the roof.

So I know for a fact that "Sub Session_OnEnd" has to run atleast once!
I have "Session.Timeout = 20" and I don't know what that means! Does it mean "Sub Session_OnEnd" runs after 20 minutes of the script being started by a user?
I hope that's for an indavidual user, for each user.

Now that must mean my Session("ID") number disapears and if it runs the "Sub Session_OnEnd" it will not find the ID number in the db to update, and either cause an error or just skip it.

Would it cause an error?
Should I add code to it to check if it's there first?

If Session("ID") <> "" Then

sSQL = "SELECT * FROM Members WHERE (ID=" & Session("ID") & ")"
Set oRS = oConn.Execute(sSQL)

If oRS.EOF <> True Then
ssSQL = "UPDATE Members SET Loggedin=False WHERE (ID=" & Session("ID") & ")"
Set ooRS = oConn.Execute(ssSQL)
End If

End If


Or something like that...

I am very confused...


I have gotten my other computer to Login and just hit exit in the broswer (not changing loggedin to false by logging out), and the session that ends should turn loggedin to false. I have set "Session.Timeout = 1", but that doesn't seem to do it anyways, what is up with this? I e-mail oracelguy, he did something like this, or exactly like this, I hope I don't bother him, and I hope he can help me.

It must do something with the session("id") turning into nothing. ""

Grrr...

whammy
11-29-2002, 06:09 AM
If someone just closes the browser, nothing is sent to the server (so your application variable will not be changed until the session times out, which by default is 20 minutes unless you change it).

From what I understand, this is a bit buggy anyway regarding classic ASP... I've seen a lot of questions on this issue and no definitive answers regarding what to do if someone doesn't "log out", and just closes their browser. I wish I could help more with it, but I haven't seen a definitive answer myself! :eek:

I know one thing I've heard (but not messed around with) is your global.asa scripts may not update unless you restart the server, since they are in use (?) This is a subject I wouldn't mind getting some definitive answers on myself...

glenngv
11-29-2002, 06:40 AM
Session_OnEnd runs when a user's session times out or quits your application. So session("id") is already destroyed when this handler is triggered.

One alternative is to used cookie to store the id instead of session.

But, as whammy said, if the user just hit the exit button, the Session_OnEnd is not executed until the default session timeout is reached. That would mean the user online status would not be updated at once.

Actually, this topic has been discussed lengthily in this thread:

http://www.codingforums.com/showthread.php?s=&threadid=980

see if you can pick up a solution for you

Morgoth
11-29-2002, 06:46 AM
Hum... That is very bother some then.
How does PHP do this?

I think I saw something like this done before in a forum for ASP, they might have a good answer on how to do it if I download the source code.

Morgoth
11-29-2002, 07:13 AM
If there is a solution out there, then I must find it!
I am sure someone has done something like this before, and there must be an answer that doesn't take millions of lines of code and mixing different code together!

Mhtml
11-29-2002, 07:41 AM
I never even thought about this. I just read that post linked above and then did a few googles but all the pages I come up with just say that users will appear to hang until the session is timed out.

As far as I can tell .net is the best alternative but I haven't learned that yet.

Sorry I couldn't be any more help but I don't think it can be done without using JS onUnload..

The only thing I found was for .net

Here's the last page I viewed..
http://www.2enetworx.com/dev/samples/memwhoson.asp
Almost at the very bottom it says what I said.

Mhtml
11-29-2002, 08:11 AM
Can you use IsClientConnected ?
I mean once it is false can you do anything, execute code?

Morgoth
11-29-2002, 02:16 PM
How does that work?
We can always test it, and check if it works.

Mhtml
11-29-2002, 11:17 PM
I had a few goes at trying to set a cookie after it was false but it didn't work, possibly I was doing something that can't be done.

But I'm thinking that the session_onEnd may work with the IsClientConnected as it is meant for that, I was just using a .asp page for this so I haven't tried the .asa

IsClientConnected

Syntax: Response.IsClientConnected
Possible values:True / False

If the client (surfer) is connected to the server then the value is true, else false.
Useful for stopping database loops if the user leaves.
eg;
do while no rs.eof and response.IsClientConnected = false

oracleguy
11-30-2002, 01:56 AM
Got your email :)

Okay what I did was... well different. I had similar problems. I worked on it for a really long time. And the best solution I came up was to have the pages check for people leaving. Now what I did is diffrent from what you are doing as far changing the dbase of logged out users. However, I think you should be able to adapt it with relative ease. The best idea would be to throw the "refresh" code into a include and just include it at the top of all your pages. Thus, when someone loads a page it checks everyones session.

So here is the code when someone enters/logs in.


Dim users, refresh
Session("name") = Request.Form("name")

If Application("NumUsers") = 0 Then
' Initialize (or re-initialize) the user list
' and the conversation
ReDim temp1(1000)
ReDim temp2(1000)
Application("users") = temp1
Application("refresh") = temp2
End If

' Retrieve the application arrays
users = Application("users")
refresh = Application("refresh")

' Find an empty spot and add the new user
Dim i
For i = 1 To 1000
If users(i) = "" Then
users(i) = Session("name")
refresh(i) = time
Application.Lock
Application("NumUsers") = Application("NumUsers") + 1
Application.Unlock
Exit For
End If
Next
If i = ubound(temp1) + 1 Then Response.Redirect "toomany.asp"

' Put the arrays back in the application vars
Application.Lock
Application("users") = users
Application("refresh") = refresh
Application.Unlock

Response.Redirect "index.asp"


What this is doing is storing the person's name, but in your case their session ID. Then it stores the time that they logged in or their "new" session began. Now you may want to remove the part for the number of users because it sounds like you already have that setup diffrently.

Now this is the code to check for the time of people's sessions:


Dim Userlist, Datelist, i, num, users, refresh

' Retrieve application arrays
users = Application("users")
refresh = Application("refresh")

' Find current user and update his refresh time
For i = 1 To 1000
If users(i) = Session("name") Then
refresh(i) = Time
Exit For
End If
Next

' Has ANYONE been gone more than 15 minutes (900 Seconds)?
For i = 1 To 1000
If users(i) <> "" Then
If DateDiff("s",refresh(i), Time) > 900 Then
'>>> SQL QUERY <<<
users(i) = ""
Application("NumUsers") = Application("NumUsers") - 1
End If
End If
Next
Application.Lock
Application("users") = users
Application("refresh") = refresh
Application.Unlock


You can run your SQL query that updates your table on the red line indicated. This should work or at least get you moving in the right direction.

Mhtml
11-30-2002, 02:19 AM
Holy crap. :D
That is such a brilliant way of doing that, I can't even really explain the ideas that just flowed into my after reading through that...

Hey Morgoth this would be for your forum correct?
If so you are almost exactly where I'm up to.
I was thinking about not having a user online thing but now...he he he :D

Morgoth
11-30-2002, 06:56 AM
First, I e-mailed the creater of the forum I looked into along time ago, and he told me his method. And I think I can shorten up your code a little if this is what you have. DiffDate or something, I will look that up later. But What he uses in his forum is the same thing.
When you login it adds a date of login. Now everytime the active users section on my site is looked at, it will check all the dates listed inside the db and check if now() and the dates in the db are off by about 10 or 15 minutes. If they are older then 10 to 15 minutes it will not add there name, but if it's younger it will add there name. This is my Timeout sulotion and it will work!

I will write up the code, and comment it, and paste it here for all you to see if you don't understand.
I will create it on saturday and paste it before sunday at sometime.

Morgoth
11-30-2002, 06:58 AM
Originally posted by Mhtml
Holy crap. :D
That is such a brilliant way of doing that, I can't even really explain the ideas that just flowed into my after reading through that...

Hey Morgoth this would be for your forum correct?
If so you are almost exactly where I'm up to.
I was thinking about not having a user online thing but now...he he he :D


This isn't for my forum as much as my site login and such, but since my forum and my site are going to merge with everything. I have already started my forum, but very slowly!
And if you look at it like the further I get the site itself done the faster I am able to work on the forums and get it done.

Mhtml
11-30-2002, 09:55 AM
Ok, So it is a portal type thing then..

I'm planning on creating a fully asp oriented community based site which people can buy and easily setup in minutes which includes a chat room, forum, link management etc.

Morgoth
11-30-2002, 06:20 PM
Sounds like a plan. But from the start, I wanted to create a website with ASP.
And when I have it finished, I will be able to understand all my code, and making something as you are would be very simple. Just because I have it all done.

If you want to create an ASP chat room, the refreshing thing might not be the best idea, you might need to use other languages.

whammy
11-30-2002, 08:21 PM
I'm going to attempt something like this in .NET before too long... I have finally started learning it, and it's awesome (from what I understand so far even!).

Morgoth
11-30-2002, 08:27 PM
How safe is .net
It's a new microsoft invention...

whammy
11-30-2002, 08:28 PM
As safe as anything else Microsoft. ;)

Actually this book delves into security quite a bit...

Morgoth
11-30-2002, 08:30 PM
Well try it out, and I can always get into it.

I wouldn't mind going with .net if it's not a HUGE change from normal ASP

whammy
11-30-2002, 08:31 PM
It's a fairly substantial change... but from what I've learned so far, it's for the better. :) Ask me in a few weeks...

Mhtml
12-01-2002, 02:31 AM
I'm getting an ASP.NET book soon as well as a PHP book, I think it is time for me to move on to .net and improve my php I'm confident in normal asp now. Took 3 months or was it 4? oh well to long in any case.

Also Whammy what is the deal with the input things like input:radio or something like that. Can you construct a complete application in pure .net code?

whammy
12-01-2002, 02:48 AM
Yup... and much quicker than in classic ASP once you really know it, from what I hear. Especially if you are using Visual Studio .NET (Which is unfortunately about $650 - $1000 depending on where you get it, though!).

For right now I'm doing all my "learning" .NET code in TextPad, and as soon as I finish the next few chapters, I'm going to try the command line compilers...

I figure it can't really hurt to learn the code "details" as opposed to using Visual Studio .NET (although I'm going to get it soon hopefully, since it does most of the work for you) - it might be a lot slower process, but in the long run I like to get "down and dirty" with the code, anyway - I might end up knowing some things that people who stick with Visual Studio .NET may not find out as quickly...

For one thing, I noticed right away by looking at the source code output that the HTML code that .NET produces in <form runat="server"> is not totally XHTML compatible (for instance, along with the "id" attribute, it inserts the "name" attribute into the form tag, and also does a few other (to me) undesirable things with HTML regarding trying to produce valid XHTML... :().

So one of my first missions as soon as I finish this book (if I don't learn how by the end of it) will be to figure out how to override the output so it IS XHTML compatible, if it's possible... :sigh:

Mhtml
12-01-2002, 03:21 AM
Which language C# or VB?

whammy
12-01-2002, 03:24 AM
Well I'm doing my coding for now in VB.NET since I mostly code ASP in VBScript (although I am paying attention to the C# syntax as well, since I like the strictness of C-type languages), but overall the syntax is pretty similar, and JScript (JavaScript pretty much) is almost exactly like C#...

...the nice thing about .NET though is if (for instance) you create a class in VB, someone coding in C# can "inherit" the class and use it in their application, and vice versa. :)

I am still hesitant to answer a lot of .NET questions though, as I'm pretty much a total newbie. I'm just learning these concepts (i.e. new ways to write "Hello World!"). ;)

Like I told Morgoth, ask me in a few weeks or a couple of months when I am starting to get a good handle on it. :D

Morgoth
12-01-2002, 04:26 AM
:D :thumbsup:

Mhtml
12-01-2002, 04:30 AM
I just looked at a tutorial for .net .. WTF? I think I'm going to enroll in a course it looks fairly complex.

Also I know what you just said but do you need VStudio.net?

whammy
12-01-2002, 04:36 AM
Well, according to the book I have (and I have done some basic things without it), no, you don't "need" it.

But apparently it saves you a ****load of time, from what I hear, since it does a lot of the things you might have to write out in detail programatically, and it's designed to be a complete IDE for .NET, encompassing all the languages, like C#, VB.NET, JScript, etc., and gives you really cool views of things like Classes, etc. etc. blah blah blah... not to mention it will compile your code for you with the click of a button once you're done with it, among other things...

I can't really give you the pros and cons of using Visual Studio .NET though, until I have used it! This is just hearsay... I want to see just what I can do without it compared to what I can do with it first. ;)

Mhtml
12-01-2002, 04:51 AM
I can see what you mean.