try another color scheme:


Go Back   TECH6.0 > Get Techie > Programming > ASP/ASP.NET


Count and show details of Active Users

This is a discussion on Count and show details of Active Users within the ASP/ASP.NET section, part of the Programming category; Create a file named global.asa that must reside at the root directory at your server i.e. htdocs OR httpdocs OR ...

View Poll Results: Did you find this code useful?
Its Perfect 6 54.55%
Just Fine 5 45.45%
Bad 0 0%
Voters: 11. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-03-2008, 09:35 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Nokia N70 Music Edition
Posts: 1,064
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default Count and show details of Active Users



Create a file named global.asa that must reside at the root directory at your server i.e. htdocs OR httpdocs OR wwwroot. Copy and paste the following code in global.asa

Code:
<object runat="Server" scope="Application"
id="rstActiveUsers" progid="ADODB.Recordset">
</object>

<script language="VBScript" runat="Server">
' The first thing you should notice is the top line.
' It creates an application scoped recordset object
' named rstActiveUsers that I'll use to store all
' our user information.
'
' Note: I've wrapped it for readability

Sub Application_OnStart()
    ' Selected constants from adovbs.inc
    Const adInteger = 3
    Const adVarChar = 200
    Const adDate = 7
    
    ' Here I set up in memory active user recordset
    ' by adding the fields I want to it and defining
    ' their data types.
    rstActiveUsers.Fields.Append "id", adInteger
    rstActiveUsers.Fields.Append "ip", adVarChar, 15
    rstActiveUsers.Fields.Append "browser", adVarChar, 255
    rstActiveUsers.Fields.Append "started", adDate

    ' Next I open our recordset so that we can use it.
    ' That basically gets everything ready for our
    ' first user.
    rstActiveUsers.Open
End Sub

Sub Session_OnStart()
    ' Set session timeout to 20 minutes
    Session.Timeout = 20

    ' Set a session start time.  This is pretty pointless,
    ' but it does ensure that we start a session and
    ' assign the user a session id and it can help
    ' troubleshooting if we ever need it.
    Session("Start") = Now()
    
    ' Move to the end so records are added in order.
    ' Again not of any real importance, but it keeps our
    ' user table nice and orderly.
    If Not rstActiveUsers.EOF Then rstActiveUsers.MoveLast

    ' Add a record and insert users data.  I'm just
    ' storing some basic info, but naturally you're free
    ' to store whatever you want.
    rstActiveUsers.AddNew
    
    rstActiveUsers.Fields("id").Value = _
        Session.SessionID
    
    rstActiveUsers.Fields("ip").Value = _
        Request.ServerVariables("REMOTE_HOST")
    
    rstActiveUsers.Fields("browser").Value = _
        Request.ServerVariables("HTTP_USER_AGENT")
    
    rstActiveUsers.Fields("started").Value = _
        Now()
    
    rstActiveUsers.Update
    
    ' Now that we've got the information, all that's
    ' left is to display it.  See test_page.asp for a
    ' demo.  It includes the pages show_count.asp and
    ' show_users.asp which can also be used
    ' individually if desired.
End Sub

Sub Session_OnEnd()
    ' Selected constants from adovbs.inc
    Const adAffectCurrent = 1

    ' Start at the first record
    rstActiveUsers.MoveFirst
    
    ' Check each record for the SessionID
    Do While Not rstActiveUsers.EOF
        ' Do conversion to Long to be sure we're
        ' comparing the same data type.
        If CLng(rstActiveUsers.Fields("id").Value) = _
            CLng(Session.SessionID) Then
            rstActiveUsers.Delete adAffectCurrent
        End If
        
        rstActiveUsers.MoveNext
    Loop
    
    rstActiveUsers.Update
End Sub


Sub Application_OnEnd()
    ' Not like it really matters, but for the sake of
    ' good coding practice I close the recordset when
    ' our application is shutting down.
    rstActiveUsers.Close
End Sub
</script>
Next, create a folder called statistics or whatever name you want where all the required files will reside.

1. Create a file named show_count.asp and copy the following code in it:-

Code:
<b><font color="#CC0000"><%= rstActiveUsers.RecordCount %></font> Active Users</b>
2. Create a file named show_users.asp and copy the following code in it:-

Code:
<%
If rstActiveUsers.RecordCount > 0 Then
    rstActiveUsers.MoveFirst

    Response.Write "<table border=""1"">" & vbCrLf

    Response.Write " <thead>" & vbCrLf
    Response.Write "  <th>Session Id</td>" & vbCrLf
    Response.Write "  <th>IP Address</th>" & vbCrLf
    Response.Write "  <th>User Agent</th>" & vbCrLf
    Response.Write "  <th>Session Start Time</th>" & vbCrLf
    Response.Write " </thead>" & vbCrLf

    Do While Not rstActiveUsers.EOF

        Response.Write " <tr>" & vbCrLf
        Response.Write "  <td>" & rstActiveUsers.Fields("id").Value & "</td>" & vbCrLf
        Response.Write "  <td>" & rstActiveUsers.Fields("ip").Value & "</td>" & vbCrLf
        Response.Write "  <td>" & rstActiveUsers.Fields("browser").Value & "</td>" & vbCrLf
        Response.Write "  <td>" & rstActiveUsers.Fields("started").Value & "</td>" & vbCrLf
        Response.Write " </tr>" & vbCrLf
        
        rstActiveUsers.MoveNext
    Loop
    Response.Write "</table>" & vbCrLf

End If
%>
3. At last, creating a file to include both the above files in it for eg. creating showstats.asp and copying the code below:-

Code:
<%@ Language=VBScript %>
<% Option Explicit %>
<html>
<head>
<title>Active Users</title>
</head>
<body>

<p>
<!-- #include file="show_count.asp" -->
</p>

<p>
<!-- #include file="show_users.asp" -->
</p>

</body>
</html>
4. Now, simply create a link to showstats.asp that displays all the info. of the current active users/visitors on your website.
__________________
Always TAG and BOOKMARK your threads
Submit your site to TECH6 Directory

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter



Reply With Quote
  #2  
Old 12-10-2008, 09:05 AM
David's Avatar

Techie
 
Name: David
Join Date: Aug 2008
Location: United States
Posts: 54
David is on a distinguished road
Default

won't that only work on windows/asp hosting?
__________________
Hey everyone! I'm David McHenry and I run a Nintendo Wii Forums
Reply With Quote
  #3  
Old 12-10-2008, 02:51 PM
webwizzy's Avatar

Administrator
 
Name: Vinayak
Join Date: Feb 2008
Location: India
Phone: Nokia N70 Music Edition
Posts: 1,064
webwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nicewebwizzy is just really nice
Send a message via Yahoo to webwizzy
Default

yes.. ofcourse it would work on just windows/asp hosting!
__________________
Always TAG and BOOKMARK your threads
Submit your site to TECH6 Directory

Would you like to Link To Us | Support TECH6 by going Premium
Know more about me at Vinayaks.com | Follow TECH6 at Twitter



Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes




All times are GMT +5.5. The time now is 08:22 PM.

Contact Us - Tech6.com - Link to Us - Advertise - Submit Site - Privacy Statement - TOS - Top