日期:2014-05-17  浏览次数:20512 次

统计阅读次数的问题
如何实现如新闻被阅读了多少,论坛贴子被阅读了多少的功能?不能是刷新一次就加一次的计数方式,另外不要对数据库造成太大压力,之前好像听说过可以累加到多少后才写数据库的。这些怎么实现?

------解决方案--------------------
C# code

 public string hits_ip;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["cip"]==null)
            {
                if (Request.ServerVariables["HTTP_VIA"] != null)
                {
                    hits_ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
                }
                else
                {
                    hits_ip = Request.UserHostAddress;
                }
                Response.Cookies.Add(new HttpCookie("cip", hits_ip));
                Response.Cookies["cip"].Expires = DateTime.Now.AddDays(1);
                string str = System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["dbPath"]);
                OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str);
                myConn.Open();
                OleDbCommand myCommand = new OleDbCommand("UPDATE [table] SET Hits=Hits+1 WHERE NewsId='" + Request.QueryString["id"] + "'", myConn);
                myCommand.ExecuteNonQuery();
                myConn.Close();
            }
        }
    }