Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 计算网站总访问者的正确方法是什么?_.net_Asp.net - Fatal编程技术网

.net 计算网站总访问者的正确方法是什么?

.net 计算网站总访问者的正确方法是什么?,.net,asp.net,.net,Asp.net,我面临着一个老问题,它让我非常困惑。所以我需要你的建议,以确保我一直在使用正确的方法。 我的要求是统计我网站的访问者数量,因此我在Global.asax文件中编码: void Application_Start(object sender, EventArgs e) { // Get total visitor from database long SiteHitCounter = 0; int CurrentUsers = 0; SiteHitCounter

我面临着一个老问题,它让我非常困惑。所以我需要你的建议,以确保我一直在使用正确的方法。 我的要求是统计我网站的访问者数量,因此我在Global.asax文件中编码:

void Application_Start(object sender, EventArgs e) 
{
    // Get total visitor from database
    long SiteHitCounter = 0;
    int CurrentUsers = 0;
    SiteHitCounter = MethodToGetTotalVisitorFromDatabase();
    Application["SiteHitCounter"] = SiteHitCounter;
    Application["CurrentUsers"] = CurrentUsers;
}

void Application_End(object sender, EventArgs e) 
{
    //  Update total visitor to database when application shutdown
    MethodToUpdateTotalVisitorToDatabase((long)Application["SiteHitCounter"]);
}

void Session_Start(object sender, EventArgs e) 
{
    // Increase total visitor and online user
    Application["SiteHitCounter"] = (long)Application["SiteHitCounter"] + 1;
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;
}

void Session_End(object sender, EventArgs e) 
{
    // Decrease online user
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
}
然后,我在另一个C#behind代码文件中使用变量应用程序[“SiteHitCounter”]和应用程序[CurrentUsers”]在网页上显示它们。 我面临的问题是,当我将网站发布到共享主机时,该网站无法在我的数据库中显示正确的总访客数

我需要你的建议

谢谢,
Tien

您不能保证会话结束事件会触发。此外,您应该调用application.lock以确保更新计数器时没有并发问题。此外,同一个人可能会在应用程序的生命周期中创建多个会话,因此您可能希望添加ip地址检查以进一步验证提高准确性

检查链接

您应该在更新之前锁定变量,因为它现在是共享的

void Session_Start(object sender, EventArgs e) 
{
    // Increase total visitor and online user
    Application.Lock();

    Application["SiteHitCounter"] = (long)Application["SiteHitCounter"] + 1;
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;

    Application.UnLock();
}

void Session_End(object sender, EventArgs e) 
{
    // Decrease online user
    Application.Lock();

    Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;

    Application.UnLock();
}

如果你想公平起见,请对ip进行一些检查,这样就没有人可以进行多个会话。

访问者是指请求一个页面的人。没有办法知道他们在请求后是否“在”你的站点上,比如他们是否正在阅读你的页面

会话从请求的第一个页面开始,20分钟后过期,即使用户在会话的第一秒钟请求了一个页面,然后离开

因此,没有真正的方法知道在某一时刻你有多少访客

您可以创建一个包含访问IP地址的列表,并注册访问时间。
然后,您可以在20分钟后使用计时器使这些条目过期。这也将使来自同一IP的重复会话无效。

perfect Azhar,我稍后将尝试执行IP检查。我现在将使用您的资料。非常感谢!欢迎……如果答案满足您的目的,请接受答案,这将有助于其他人完成检查请为他们的简单问题选择正确的方法。感谢您必须使用并让他们处理此问题:)