统计匿名和登录用户- ASP.网的网站

本文关键字:ASP 网站 用户 登录 统计 | 更新日期: 2023-09-27 18:08:56

我想实现可靠的用户计数器。目前我正在使用会话变量。下面是我的实现:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;
    Application["LoggedInUsers"] = 0;
}
void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}
void Session_End(object sender, EventArgs e) 
{
    Application.Lock();
    if ((int)Application["OnlineUsers"] > 0)
    {
        Application["OnlineUsers"] = (int) Application["OnlineUsers"] - 1;
    }
    Application.UnLock();
}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
    Application.Lock();
    Application["LoggedInUsers"] = (int)Application["LoggedInUsers"] + 1;
    Application.UnLock();
}
protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
{
    Application.Lock();
    if ((int)Application["LoggedInUsers"] > 0)
    {
        Application["LoggedInUsers"] = (int) Application["LoggedInUsers"] - 1;
    }
    Application.UnLock();
}

这种方法的缺点是众所周知的。我找不到更准确的说法了。你能帮我一下吗?

统计匿名和登录用户- ASP.网的网站

考虑到Session_End的一些模糊性质,我使用的方法是将"在线用户"定义为在过去5分钟内有活动的人。然后你就可以计算在该时间段内有活动的用户数量。