程序是这样的,
我做了一个在线用户统计的功能:(从数据库中某字段的状态获取)
但是这种方法不准确!
只有当用户正常退出,就是点网页上的一个退出按钮后,程序才能正常统计在线用户数
但是如果用户是直接关闭IE,或其他一些方式退出的话。
其数据库的状态还一直是在线状态(其实已经离线了)
这就影响了在线用户统计的真实性。
请问如何解决这个问题?

解决方案 »

  1.   

    在 session_start 和 session_end 里面统计啊
      

  2.   

    <sessionState 
                mode="InProc"
                stateConnectionString="tcpip=127.0.0.1:42424"
                sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
                cookieless="false" 
                timeout="20" 
        />
    这是默认web.config里会话状态的配置,timeout="20"表示20分钟,用户关闭浏览器当然不会中止会话,用户20分钟不做任何操作才会中止。
    一般都是根据会话统计,好像没有别的更好办法
      

  3.   

    你找个文章管理系统看看源码,现在的很多论坛也有这个功能的.http://gameshop.net.cn/book/083149b620.htm
    http://www.a1bc.com/InfoView/Article_39847.html
      

  4.   

    给你转个贴子,希望对你有所帮助http://community.csdn.net/Expert/topic/3892/3892945.xml?temp=.0244562
    Eddie005(♂) 暴赱 『零零伍』大侠的方法 
    总的来说,要做个在线人数统计简单,但是要做在线名单并且保存用户的访问日志,就需要耗费比较多的系统资源,是否划算就难说了(我只看需求文档,其他不管...);前面用过的IHttpModule方法也不错,原先每用过,也学了一招... 感谢思归老大的帮忙,分就散了吧~using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Data;
    using System.Data.OleDb;namespace XsExam
    {
    /// <summary>
    /// Global 的摘要说明。
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
            private static System.Threading.Timer timer;
            private const int interval = 1000 * 60 * 10;//检查在线用户的间隔时间
            
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;public Global()
    {
    InitializeComponent();
    }protected void Application_Start(Object sender, EventArgs e)
    {
                if (timer == null)
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(ScheduledWorkCallback),
                        sender, 0, interval);            DataTable userTable = new DataTable();
                userTable.Columns.Add("UserID");//用户ID
                userTable.Columns.Add("UserName");//用户姓名
                userTable.Columns.Add("FirstRequestTime");//第一次请求的时间
                userTable.Columns.Add("LastRequestTime");//最后一次请求的时间
                userTable.Columns.Add("ClientIP");//
                userTable.Columns.Add("ClientName");//
                userTable.Columns.Add("ClientAgent");//
                //userTable.Columns.Add("LastRequestPath");//最后访问的页面            userTable.PrimaryKey = new DataColumn[]{userTable.Columns[0]};
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();
    }
     
    protected void Session_Start(Object sender, EventArgs e)
    {        }        protected void Application_BeginRequest(Object sender, EventArgs e)
    {
                
    }protected void Application_EndRequest(Object sender, EventArgs e)
    {}        protected void Application_AcquireRequestState(Object sender, EventArgs e)
            {
                HttpApplication mApp = (HttpApplication)sender;
                if(mApp.Context.Session == null) return;
                if(mApp.Context.Session["UserID"]==null ) return;
                string userID = mApp.Context.Session["UserID"].ToString();
                
                DataTable userTable = (DataTable)Application["UserOnLine"];
                DataRow curRow = userTable.Rows.Find(new object[]{userID});
                if(curRow != null)
                {
                    this.GetDataRowFromHttpApp(mApp,ref curRow);
                }
                else
                {
                    DataRow newRow = userTable.NewRow();
                    this.GetDataRowFromHttpApp(mApp,ref newRow);
                    userTable.Rows.Add(newRow);
                }
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();        }protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
               
    }protected void Application_Error(Object sender, EventArgs e)
    {
                
    }       
    protected void Session_End(Object sender, EventArgs e)
    {
                
    }protected void Application_End(Object sender, EventArgs e)
    {
                
    }#region Web 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
                this.components = new System.ComponentModel.Container();
                
            }
    #endregion        private void GetDataRowFromHttpApp(HttpApplication mApp,ref DataRow mRow)
            {
                if(mApp.Context.Session == null) return;
                if(mApp.Context.Session["UserID"]==null || mApp.Context.Session["UserName"]==null) return;
                string userID = mApp.Context.Session["UserID"].ToString();
                string userName = mApp.Context.Session["UserName"].ToString();
                //string requestPath = mApp.Request.Path;
                
                if(mRow["UserID"].ToString().Length<1)
                {
                    mRow["UserID"]      = userID;
                    mRow["UserName"]    = userName;
                    mRow["FirstRequestTime"] = System.DateTime.Now;
                    mRow["ClientIP"]    = mApp.Context.Request.UserHostAddress;
                    mRow["ClientName"]  = mApp.Context.Request.UserHostName;
                    mRow["ClientAgent"] = mApp.Context.Request.UserAgent;
                }            mRow["LastRequestTime"] = System.DateTime.Now;
                //mRow["LastRequestPath"] = requestPath;
                
            }        private void ScheduledWorkCallback (object sender)
            {
                string filter = "Convert(LastRequestTime,'System.DateTime') < Convert('" + System.DateTime.Now.AddSeconds(-interval/1000).ToString() + "','System.DateTime')";
                DataTable userTable = (DataTable)Application["UserOnLine"];
                DataRow[] lineOutUsers = userTable.Select(filter);
                for(int i=0;i<lineOutUsers.Length;i++)
                {
                    DataRow curRow = lineOutUsers[i];
                    
                    //保存到数据库
                    XsStudio.Database db = new XsStudio.Database();
           curRow.Delete();
                    
                }
                userTable.AcceptChanges();            Application.Lock();
                Application["UserOnLine"] = userTable;
                Application.UnLock();
            }       
    }
    }
      

  5.   

    http://community.csdn.net/Expert/topic/4137/4137928.xml?temp=.623974