我用的是asp.net (c#) 请问我想在网页上添加登陆人数 我该怎么着手呢? 比如 第一个登陆成功后为1 第二个为1+1=2
谢谢指教
谢谢指教
解决方案 »
- odp.net 创建 触发器的问题
- 问个很菜的问题,<%#的用法是什么?
- 为什么动态注册javascript之前需要判断if(!ClientScript.IsClientScriptRegistered())?
- 请问关于在masterpage中使用treeview的使用
- 我在.net2003上面做好的网站,在.net2005上运行出错
- 百分求教:如何做才可将freetextbox中的图片上传到服务器?(急啊)
- 关于字符串的问题!简单
- 如何在datagrid里添加一个时间控件
- 缩略图问题!
- 使用AspNetMenu控件,菜单中文字体显示有问题!顺便发布IT公司速查手册,新的一年找工作必看!
- 字体大小问题
- 复写虚函数问题??
也可以这样
一、原理
在.net中的global.asax中有Application_AuthenticateRequest事件和Application_BeginRequest事件是在每次访问aspx文件都会触发。但是Application_BeginRequest中不能对已经经过FROMS身份验证的身份ticket票进行识别。所以只能放到Application_AuthenticateRequest中去。 实现原理是:每次访问aspx文件时候都会判断在线表里面是否有这个用户(已经登录了的记录用户名,没有登录的记录IP地址),如果不存在,则将该用户的身份、最后访问时间、最后访问IP、和最后访问的URL存入数据库。如果数据库中已经曾在,则更新该记录,把最后访问时间,IP以及最后访问URL更新。 同时,删除数据库中与当前时间间隔20分钟以上的数据(20分钟没操作当为超时)。二、优点
这样,你不仅仅可以看到当前在线的准确人数,还知道是那些人在线,以及是否登陆,和访问人数中已经是会员的比例,以及所在位置,并计算某个页上的人数。三、数据库结构:
字段 类型 长度 说明
1uson_serial int 40 序号
0uson_user varchar 200 用户名(没登陆则为IP)
0uson_company varchar 1000 公司名(没登陆则为'游客')
0uson_ip varchar 200 IP地址
0uson_date datetime 80 最后操作时间
0uson_url varchar 1000 最后操作页面路径
四、程序
注意:
1、程序位于global.asax中
2、使用的FORMS身份验证
3、请使用 System.Web.Security
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string strUserID = string.Empty;
string strCompany = string.Empty;
if (Request.IsAuthenticated)
{
FormsIdentity identity = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = identity.Ticket;
strUserID = User.Identity.Name;
strCompany = ticket.UserData.Split("|".ToCharArray())[2];
}
else
{
strUserID = Request.UserHostAddress;
strCompany = "游客";
}MemberOnlineInfo objOnline = new MemberOnlineInfo(strUserID, Request.UserHostAddress, DateTime.Now.ToString(), Request.FilePath, strCompany);MemberAccount account = new MemberAccount();
if (!account.CheckUserOnline(strUserID))
account.AddOnline(objOnline);
else
account.UpdateOnline(objOnline);//删除超时的会员
account.DeleteOnline();
}
如果你自己写的用户管理机制,可以把每个登录用户的信息放在session里,去相应session的count不就行了
<head runat="server">
<title>BarCode System Control Management</title>
</head>
<body>
<form id="form1" runat="server">
<table style="height: 57px" >
<tr>
<td style="width: 802px; height: 18px">
<img src="Images/Index01_1.gif" /></td>
</tr>
</table>
<TABLE id="Table2" style="WIDTH: 807px; height: 429px;" borderColor="silver" cellSpacing="0"
cellPadding="1" border="1">
<tr ><td style="vertical-align: text-top; width: 602px; height: 433px; text-align: center;" align="center">
<span style="font-family: Arial">Welcoming.................
<br />
</span>
<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
BorderWidth="1px" FailureText="登入失敗!!請確認您的帳號與密碼是否正確,大小寫視為不同的字元" Font-Names="Verdana"
Font-Size="10pt" RememberMeText="記住我的帳號"
TitleText="使用者登入" UserNameLabelText="帳號:" OnAuthenticate="Login1_Authenticate">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<LayoutTemplate>
<table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="height: 153px">
<table border="0" cellpadding="0">
<tr>
<td align="center" colspan="2" style="font-weight: bold; color: white; height: 30px;
background-color: #6b696b">
使用者登入</td>
</tr>
<tr>
<td align="right" style="width: 42px">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">帳號:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="必須提供使用者名稱。" ToolTip="必須提供使用者名稱。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" style="width: 42px">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密碼:</asp:Label></td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="100px"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="必須提供密碼。" ToolTip="必須提供密碼。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<%-- <td colspan="2">
<asp:CheckBox ID="RememberMe" runat="server" Text="記住我的帳號" />
</td>--%>
</tr>
<tr>
<td align="center" colspan="2" style="color: red; height: 16px;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登入" ValidationGroup="Login1" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login> <br />
<br />
<br />
<br />
<br />
</td>
<td style="width: 897px; height: 433px;">
<div style="text-align: center"><FONT face="新細明體" size=2>
<img src="Images/LCDMCS.gif" style="width: 601px; height: 416px" /></FONT></div>
</td>
</tr>
</TABLE><table style="height: 51px" >
<tr>
<td style="width: 807px; height: 18px">
<img src="Images/Index02.gif" /></td>
</tr>
</table>
</form>
</body>
</html>
以上为我的登陆界面的 aspx 文件 我想在上面加登陆成功的人数而不是在线人数
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class Login : System.Web.UI.Page
{
private SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["LCDMCS_conn"]); protected void Page_Load(object sender, EventArgs e)
{
} protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
//Response.Write(Login1.UserName);
//Response.Write(Login1.Password); if (UserPassWordCheck(Login1.UserName, Login1.Password))
{
e.Authenticated = true; FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
else
{
e.Authenticated = false;
}
}
private bool UserPassWordCheck(string UserId, String PassWord)
{
bool ReturnValue = false; // 建立Command的方式二
SqlCommand Scomm = new SqlCommand("SELECT * FROM EMPLOY WHERE LTRIM(RTRIM(USERID)) = @UserId AND LTRIM(RTRIM(PASSWD)) = @PassWord", Conn);
Scomm.Parameters.Clear();
Scomm.Parameters.Add("@UserId", SqlDbType.NChar).Value = UserId;
Scomm.Parameters.Add("@PassWord", SqlDbType.NChar).Value = PassWord;
//
Conn.Open(); SqlDataReader Dr = Scomm.ExecuteReader(); if (Dr.Read())
{
HttpCookie LoginCookie = new HttpCookie("USERID");
LoginCookie.Values.Add("USERID", Login1.UserName);
Response.Cookies.Clear();
Response.Cookies.Add(LoginCookie);
ReturnValue = true;
}
else
{
ReturnValue = false;
} Dr = null;
Scomm = null;
Conn = null; return ReturnValue;
}
}
这个是 .cs 文件