ASP.NET Who‘s Online Database Example
2008-05-12 00:17:47.0
This article is in response to the question that was sent to the ASP Allianc
e article request database:
"I have an application where everytime a user logs on a session is started.
As an admin or even as a user view, I would like to be able to display all o
f the users (e.g. Session("usernames")) on a site status/activity page. Have
 you seen this before ? Anyone know how or similar idea ?"
I have done this, on AlexASP!  I have also done it in both Classic ASP and A
SP.NET.
This is the ASP.NET version of the application.  To see how to do the old AS
P one, click here.  First, we define the data structure:
CREATE TABLE AlexASP_CurrentUsers  (IPAddress varchar(100), UserBrowser varc
har(100), Referer varchar(250), SessionID varchar(250), CurrentPage varchar(
250))
Then, we need to get Global.asax to add each new user to the database as the
y enter the page (we do this in the Session_OnStart event) and delete them a
s their session finishes (in the Session_OnEnd event).
Here's my global.asax:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart()
Dim sIPAddress, sReferer, sBrowser, sSessionID
sIPAddress = Request.ServerVariables("REMOTE_ADDR")
sReferer = Request.ServerVariables("HTTP_REFERER")
sBrowser = Request.ServerVariables("HTTP_USER_AGENT")
sSessionID = Session.SessionID
Dim oConnection as New SqlConnection(ConfigurationSettings.AppSettings("aspa
_ConnectionString"))
Dim oCommand as New SqlCommand("Insert Into AlexASP_CurrentUsers(IPAddress,
UserBrowser, Referer, SessionID) VALUES ('" & sIPAddress & "', '" & sBrowser
 & "', '" & sReferer & "', '" & sSessionID & "')", oConnection)
oCommand.connection.Open()
oCommand.executenonquery()
oCommand.connection.Close()
oConnection.close
End Sub
Sub Session_OnEnd()
Dim oConnection as New SqlConnection(ConfigurationSettings.AppSettings("aspa
_ConnectionString"))
Dim oCommand as New SqlCommand("DELETE FROM AlexASP_CurrentUsers WHERE Sessi
onID = '" & Session.SessionID & "'", oConnection)
oCommand.connection.Open()
oCommand.executenonquery()
oCommand.connection.Close()
oConnection.close
End Sub
</SCRIPT>
Then, we need the include file:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<SCRIPT LANGUAGE="VB" RUNAT="SERVER">
Sub Update_CurrentPage()
Dim oConnection as New SqlConnection(ConfigurationSettings.AppSettings("aspa
_ConnectionString"))
Dim oCommand as New SqlCommand("UPDATE AlexASP_CurrentUsers Set Current_Page
='" & Request.ServerVariables("SCRIPT_NAME") & "' WHERE SessionID='" & Sessi
on.SessionID & "'", oConnection)
oCommand.connection.Open()
oCommand.executenonquery()
oCommand.connection.Close()
End Sub
</SCRIPT>
The file above must be included in all the pages you wish to track.  You can
 include it and run the update procedure with this code:
<!-- #include virtual="/alexcampbell/includes/whoson_include.aspx" -->
<% Call Update_CurrentPage %>
Then, to view the users that are currently online, you make this page (click
 here to see it in operation):
<!-- #include virtual="/alexcampbell/includes/article_inc.aspx" -->
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
MyConnection = New SqlConnection(ConfigurationSettings.AppSettings("aspa_Con
nectionString"))
MyCommand = New SqlDataAdapter("select IPAddress, UserBrowser, Current_Page
from AlexASP_CurrentUsers", MyConnection)
DS = new DataSet()
MyCommand.Fill(ds, "AlexASP_CurrentUsers")
MyDataGrid.DataSource=ds.Tables("AlexASP_CurrentUsers").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<% Call AlexASP_Write_Header("AlexASP Current Online Users","View Current Us
ers") %>
<p align="center">Welcome to
AlexASP Who's Online Example.&nbsp; This page shows all the users
that are online, and their IP address, and the page they are
currently viewing.&nbsp; <a href="currentusers_code.asp" target="_blank">Cli
ck
here</a> to view the source code of this page...</p>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="400"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
<% Call AlexASP_Write_Footer %>
If you want to be able to list the online (registered) users, then in your l
ogon page, add the code above, substituting the Request.ServerVariables("REM
OTE_ADDR") for Session("UserName").
After customizing both pages to this spec you will have a list of every user
 currently online.
If you have any questions/comments/critisisms, please feel free to contact m
e by email: alex@aspalliance.com


本文引用的地址为http://www.xici.net/b231526/d47463376.htm
文章评论
[以下网友留言只代表其个人观点,不代表中华网的观点或立场]
发表评论
昵 称:
主 页:
内 容:
表 情: