zoukankan      html  css  js  c++  java
  • 统计在线人数

    <%@ Import Namespace="System.Data"%>
    void Application_Start(object sender, EventArgs e) 
        {
           //在应用程序启动时运行的代码,网站启动时即出发此事件
            try
            {
    
                DataTable userTable = new DataTable();
                userTable.Columns.Add("SessionID");
                userTable.Columns.Add("UserIP");
                userTable.Columns.Add("Browser");
                userTable.Columns.Add("OSName");
    
                userTable.AcceptChanges();
                Application.Lock();
                Application["OnLineusers"] = userTable;
                Application.UnLock();
        }
        void Session_Start(object sender, EventArgs e) 
        {
            //在新会话启动时运行的代码,用户访问时会触发Session_Start事件,在Session_Start中记录下该用户的信息,存入表中,
    //Application["OnLineusers"]存放了所有用户的信息
    string sessionid = Session.SessionID; string userIP = Request.UserHostAddress; HttpBrowserCapabilities bc = Request.Browser; string osName = bc.Platform; string Browser=bc.Type; DataTable userTable = (DataTable)Application["OnLineusers"]; if (userTable == null) return; DataRow[] curRow=userTable.Select("SessionId='"+sessionid+"'"); if (curRow.Length == 0) { DataRow newRow = userTable.NewRow(); newRow["SessionID"] = sessionid; newRow[1] = userIP; newRow[2] = Browser; newRow[3] = osName; userTable.Rows.Add(newRow); userTable.AcceptChanges(); Application.Lock(); Application["OnLineusers"] = userTable; Application.UnLock(); } }
       //移除回话结束的Session
      void Session_End(object sender, EventArgs e) { Hashtable onlineuserhash = (Hashtable)Application["OnLineusers"]; onlineuserhash.Remove(Request.UserHostAddress); string sessionid = Session.SessionID; DataTable usertable = (DataTable)Application["OnLineusers"]; if (usertable == null) { return; } foreach (DataRow row in usertable.Select("SessionID='" + sessionid + "'")) { usertable.Rows.Remove(row); } usertable.AcceptChanges(); Application.Lock(); Application["OnLineusers"] = usertable; Application.UnLock(); }

     常用的解决方法:在项目下建一个txt文件存储在线人数,Session_Start,Session_end对txt进行操作

  • 相关阅读:
    Win下循环进入目录启动执行某任务
    Mysql数据库搭建-Windows
    Linux的服务器初始优化脚本。
    Linux下Find命令的使用
    一些判断Linux是否被黑的经验
    搭建docker私有仓库
    进程退出:SIGINT、SIGTERM和SIGKILL区别
    dockerfile使用
    k8s-ingress安装
    k8s-service
  • 原文地址:https://www.cnblogs.com/huangll/p/2782888.html
Copyright © 2011-2022 走看看