命里有时终须有,命里无时莫强求

进入职业的另个阶段,同时也是寻找下一阶段的方向!

 

用asp.net显示在线登陆人数及位置

一、原理

  在.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();

}

posted on 2008-11-25 16:30  刘蔡涛  阅读(123)  评论(0编辑  收藏  举报

导航