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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seesion Problem (Active users)

Status
Not open for further replies.

Ragol1

Programmer
Oct 25, 2001
315
US
Ok this may be long but here it is, I got this code from a forum and it works ok displaying the results on a page, but I want to add the username so it displays who is logged in.
PLease help I have added 2 lines '-------

But it doesnt work

<object runat=&quot;Server&quot; scope=&quot;Application&quot;
id=&quot;rstActiveUsers&quot; progid=&quot;ADODB.Recordset&quot;>
</object>

<script language=&quot;VBScript&quot; runat=&quot;Server&quot;>

Sub Application_OnStart()
Const adInteger = 3
Const adVarChar = 200
Const adDate = 7


rstActiveUsers.Fields.Append &quot;id&quot;, adInteger
rstActiveUsers.Fields.Append &quot;ip&quot;, adVarChar, 15
rstActiveUsers.Fields.Append &quot;browser&quot;,adVarChar,25
'---I added this------
rstActiveUsers.Fields.Append &quot;UserID&quot;, adVarChar, 255
rstActiveUsers.Fields.Append &quot;started&quot;, adDate
rstActiveUsers.Open
End Sub

Sub Session_OnStart()
Session.Timeout = 20

Session(&quot;Start&quot;) = Now()

If Not rstActiveUsers.EOF Then rstActiveUsers.MoveLast



rstActiveUsers.AddNew

rstActiveUsers.Fields(&quot;id&quot;).Value = _
Session.SessionID

rstActiveUsers.Fields(&quot;ip&quot;).Value = _
Request.ServerVariables(&quot;REMOTE_HOST&quot;)

rstActiveUsers.Fields(&quot;browser&quot;).Value = _
Request.ServerVariables(&quot;HTTP_USER_AGENT&quot;)

rstActiveUsers.Fields(&quot;started&quot;).Value = _
Now()

'----------I added this but no Luck ---------
rstActiveUsers.Fields(&quot;UserID&quot;).Value = _
session(&quot;UserID&quot;)

rstActiveUsers.Update

End Sub

Sub Session_OnEnd()
Const adSearchForward = 1
Const adBookmarkFirst = 1
Const adAffectCurrent = 1

rstActiveUsers.Find &quot;id = &quot; & Session.SessionID, _
0, adSearchForward, adBookmarkFirst

If Not rstActiveUsers.EOF Then
rstActiveUsers.Delete adAffectCurrent
End If
End Sub

Sub Application_OnEnd()

rstActiveUsers.Close
End Sub
</script>


Then On show_users.asp
<%
If rstActiveUsers.RecordCount > 0 Then
rstActiveUsers.MoveFirst

Response.Write &quot;<table border=&quot;&quot;1&quot;&quot;>&quot; & vbCrLf

Response.Write &quot; <thead>&quot; & vbCrLf
Response.Write &quot; <th>User Name</td>&quot; & vbCrLf
Response.Write &quot; <th>Session Id</td>&quot; & vbCrLf
Response.Write &quot; <th>Session Start Time</th>&quot; & vbCrLf
Response.Write &quot; </thead>&quot; & vbCrLf

Do While Not rstActiveUsers.EOF

Response.Write &quot; <tr>&quot; & vbCrLf
Response.Write &quot; <td>&quot; & rstActiveUsers.Fields(&quot;UserID&quot;).Value & &quot;</td>&quot; & vbCrLf
Response.Write &quot; <td>&quot; & rstActiveUsers.Fields(&quot;id&quot;).Value & &quot;</td>&quot; & vbCrLf
Response.Write &quot; <td>&quot; & rstActiveUsers.Fields(&quot;started&quot;).Value & &quot;</td>&quot; & vbCrLf
Response.Write &quot; </tr>&quot; & vbCrLf

rstActiveUsers.MoveNext
Loop
Response.Write &quot;</table>&quot; & vbCrLf

End If
%>
 
Where do you assign a value to session(&quot;UserID&quot;)?
What is your source of User Names?
How do you determine which user is using your application?

Where do they login? Would it be at the network login?

The Session_OnStart() subroutine runs when the browser first requests an ASP page in your application. So I don't think it can ever get values from Session variables, although it can assign values to them.
 
During the login process I create a cookie for UserID

All the usernames are in a DB but I allow cookies on correct logins

Dont know about the rest
 
So you have a form where a person enters their username.
The form is submitted.
The form data is handled by an ASP script. The username and password are matched against the DB by your script.
When your script determines that this login is OK put the username into the recordset.

Code:
'Get the username from the form.
Dim varUsername
varUsername = Request.Form(&quot;username&quot;).Item()
 . . .
'check the username against the DB
 . . .
'If the user is OK put the username into the recordset.
'First find the row in the recordset for this session.
rstActiveUsers.MoveFirst
rstActiveUsers.Find &quot;id = &quot; & Session.SessionID, _
        0, adSearchForward, adBookmarkFirst
    
rstActiveUsers.Fields(&quot;UserID&quot;).Value = varUsername
rstActiveUsers.Update
 . . .

Let me know if this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top