您的位置:首页 > 其它

如何统计在线人数

2013-04-24 11:21 309 查看
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        string filepath = Server.MapPath("~/App_Data/Count.txt");
        if (!File.Exists(filepath))
        {
            StreamWriter sw = new StreamWriter(filepath);
            sw.WriteLine(0);
            sw.Flush();
            sw.Close();
        }
        StreamReader sr = new StreamReader(filepath);
        int intFwl = int.Parse(sr.ReadLine());
        sr.Close();

        Application["Fwl"] = intFwl;
        Application["Online"] = 0;
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
        string filepath = Server.MapPath("~/App_Data/Count.txt");
        StreamWriter sw = new StreamWriter(filepath);
        sw.WriteLine(Application["Fwl"]);
        sw.Flush();
        sw.Close();
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码
        Application["Fwl"] = int.Parse(Application["Fwl"].ToString()) + 1;
        Application["Online"] = int.Parse(Application["Online"].ToString()) + 1;
        Session.Timeout = 5;
        string filepath = Server.MapPath("~/App_Data/Count.txt");
        StreamWriter swFile = new StreamWriter(filepath);
        swFile.WriteLine(Application["Fwl"]);
        swFile.Flush();
        swFile.Close();
    }

    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。
        Application["Online"] = int.Parse(Application["Online"].ToString()) - 1;
    }
</script>

在线 和 来访人数 上面全体复制到 Global.asax

分割线----------------------------------------------------------------------------------------------

下面是显示出来的 添加到你需要的页的cs文件里面

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sFwl = Application["Fwl"] != null ? Application["Fwl"].ToString() : "N";
            string sOnline = Application["Online"] != null ? Application["Online"].ToString() : "N";
            for (int i = 0; i < sFwl.Length; i++)
            {
                Label1.Text += "<img src='images/count/" + sFwl[i] + ".png'/>";
            }
            for (int i = 0; i < sOnline.Length; i++)
            {
                Label2.Text += "<img src='images/count/" + sOnline[i] + ".png'/>";
            }
        }
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: