最简单的访问统计

最简单的访问统计

在网站中新建一个global.asax文件,使用全局变量 Application,start,end,Session_start,Session_end事件统计数量,使用数据库单独的一个表保存记录或在app_data文件夹建立一个xml文件保存

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码
        int todayCount = 0;//今天在线人数      
        string stSql = "select * from tongji where tcid = 1";
        System.Data.DataSet ds = Maticsoft.DBUtility.DbHelperOleDb.Query(stSql);
        Application["tcCount"] = ds.Tables[0].Rows[0]["tcCount"].ToString();  //总访问人数
        Application["tcVisit"] = todayCount ;       //当前在线人数
    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码
        Application.Lock();

    int todayCount = 0;//今天访问数
        long totalcount = long.Parse(Application["tcCount"].ToString());  //总访问人数
        totalcount++;  //用户访问打开页面后修改访问总数

    todayCount++;  //在线用户数量加 1
        Application["tcCount"] = totalcount;

    Application["tcVisit"] = todayCount ;       //当前在线人数
        string stSql = "update tongji set tcCount = "+totalcount+" where tcid=1";
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)

    {   //这里主要计算登录用户

        Application.Lock();

        Application["tcVisit"] = int.Parse(Application["tcVisit"].ToString()) - 1;

        //当用户离开站点的时候,在线总数减1

        Application.UnLock();

  }

原文地址:https://www.cnblogs.com/sgivee/p/1815108.html