看看这个,有帮助不using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState; using System.IO ;namespace movie { /// <summary> /// Global 的摘要说明。 /// </summary> public class Global : System.Web.HttpApplication { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; public Global() { InitializeComponent(); }
大家都知道 web 页,是无状态的。要更准确地统计人员在线。我这是样做的。先取得来访者的IP和当前时间 写入数据库 在线人数统计就是统计数据库的IP条数。然后每当有用户进入此网站时,就把。 delete from useronline where 当前时间-30分钟>以前写入的时间delete from useronline where datediff(minute,cometime,getdate())>=30";//第一次来的IP删除过时的IP本人觉得这样做,比较准确统计在线人数
用户2登录 ====》此用户保存在SESSION里 然后 服务器定时扫描SESSION?然后我怎么判断是否在线? 定时扫描数据库?还是可以扫描 session 还是 登录存到 application 定时扫描 application?
没增加一个session就会触发一个事件 你就可以记录
session消失的时候也会触发另外一个事件c#里应该有类似的吧
id u_id login_time action2\列表查询user_login实现在线人员列表3\关闭 IE时删除user_login表中对应u_id的记录.(这个可以实现)
login_end 这一字段id 自增
u_id 用户id
login_time 登录时间
login_end 最后活动时间
action 当前活动如mrlen, 可以设置当now - login_end >= 15分钟(自定义值)时,删除用户user_login表中对应记录,强制用户退出系统.意为此用户离线.
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO ;namespace movie
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
Application["conn"]="Server=localhost;database=movie;uid=sa;pwd='zcc';";
Application["user_sessions"] = 0;
Application["counter_num"]=0;
uint count=0;
StreamReader srd;
//取得文件的实际路径
string file_path=Server.MapPath ("counter.txt");
//打开文件进行读取
srd=File.OpenText (file_path);
while(srd.Peek ()!=-1)
{
string str=srd.ReadLine ();
count=UInt32.Parse (str);
}
object obj=count;
Application["counter"]=obj;
srd.Close (); } protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] + 1;
Application.UnLock(); Application.Lock ();
//数值累加,注意这里使用了装箱(boxing)
uint jishu=0;
jishu=(uint)Application["counter"];
jishu=jishu+1;
object obj=jishu;
Application["counter"]=obj;
//将数据记录写入文件
string file_path=Server.MapPath ("counter.txt");
StreamWriter fs=new StreamWriter(file_path,false);
fs.WriteLine (jishu);
fs.Close ();
Application.UnLock ();
} protected void Application_BeginRequest(Object sender, EventArgs e)
{// Application.Lock();
// Application["counter_num"]=(int)Application["counter_num"]+1;
// Application.UnLock(); } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
Application.UnLock(); } protected void Application_End(Object sender, EventArgs e)
{ uint js=0;
js=(uint)Application["counter"];
//object obj=js;
//Application["counter"]=js;
//将数据记录写入文件
string file_path=Server.MapPath ("counter.txt");
StreamWriter fs=new StreamWriter(file_path,false);
fs.WriteLine(js);
fs.Close ();
}
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}
根据session的状态修改这个值,哪里要用就直接查询。
我是这么弄的。
2.关闭IE是会有事件响应,但是右键任务栏的IE关闭,没有提示.
3.按标签分栏里的交叉关闭页面好像也没事件响应.
4.突然停电.....也不会有提示....
每次登陆进来,就直接标记为在线,并记录用户id在session
在global.ascx里面void Session_End(object sender, EventArgs e) 方法里面写逻辑,将id所对应的用户状态给改变为不在线