<%@ Application Language="C#" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        Application["total"] = 0;
        Application["EveryIP"] = "";
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
        Session.Clear();
        Application.Clear();
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码
        Application["total"] = 0;
        Application["EveryIP"] = "";
        Session["ad"] = "";
        Session["oldad"] = "";
        Session["pw"] = "";
        Session["profile"] = "";
        Session["ip"] = Request.UserHostAddress.ToString();
    }

    void Session_Start(object sender, EventArgs e) 
    {

        // 在新会话启动时运行的代码
        cookie mycookie = new cookie();
        if (mycookie.dr_cookie("wormcook", "ad") != "" && mycookie.dr_cookie("wormcook", "pw") != "")
        {
            //读取客户的cookie
            Session["ad"] = mycookie.dr_cookie("wormcook", "ad");
            Session["oldad"] = mycookie.dr_cookie("wormcook", "ad");
            Session["pw"] = mycookie.dr_cookie("wormcook", "pw");
            Session["profile"] =mycookie.dr_cookie("wormcook", "file");
        }
        else
        {
            Session["ad"] = "";
            Session["oldad"] = "";
            Session["pw"] = "";
            Session["profile"] = "";
        }
        Session["ip"] = Request.UserHostAddress.ToString();
        Session.Timeout = 30;
        //对Appliaction加锁以防止并行性
        //锁上
        //-初始Application["EveryIP"]，第一次为Application["EveryIP"]赋值IP地址和用户名
        if (Application["EveryIP"].ToString().IndexOf(Request.UserHostAddress.ToString()) < 0)
        {
            string ad;
            if (Session["ad"].ToString() == "")
            { ad = "游客"; }
            else
            { ad = Session["ad"].ToString(); }
            string tempip = ad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";
            Application.Lock();
            Application["EveryIP"] = Application["EveryIP"].ToString() + tempip;
            Application["total"] = (int)Application["total"] + 1;    //增加一个在线人数
            Application.UnLock();//解锁
        }
        
        //-end
        //End
        
    }

    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer，则不会引发该事件。
        Application.Lock();
        if ((int)Application["total"]> 1)
        {
            Application["total"] = (int)Application["total"] - 1;  //减少一个在线人数  
            Application["EveryIP"] = Application["EveryIP"].ToString().Replace(Request.UserHostAddress.ToString(), "离开状态");
        }
        
        Application.UnLock();//解锁
    }
       
</script>
