怎样做文章系统中的防刷新计数器?该怎么处理
怎样做文章系统中的防刷新计数器?
普通的计数器防刷新,方法较多。
如果是文章系统,每篇文章的阅览人数都有统计,这个怎样防止刷新计数?
在用户的session期内,两篇文章,每篇文章都要加1。看同一篇文章,看多少遍都只加1次。
这个该怎么实现呢?
------解决方案--------------------
根据IP和时间来决定是否计数,总数会少一些,因为共用一个IP的按照一个IP计数。
12:00:00 222.123.123.25看了一次 计数
通过服务器对象application也可以
------解决方案--------------------
一个用户看过的文章的ID,可以放一个Session变量中
刷新计数时,检查这个用户是否看过。
------解决方案--------------------
防刷新的话用ajax了
------解决方案--------------------
------解决方案--------------------
Global.asax
普通的计数器防刷新,方法较多。
如果是文章系统,每篇文章的阅览人数都有统计,这个怎样防止刷新计数?
在用户的session期内,两篇文章,每篇文章都要加1。看同一篇文章,看多少遍都只加1次。
这个该怎么实现呢?
------解决方案--------------------
根据IP和时间来决定是否计数,总数会少一些,因为共用一个IP的按照一个IP计数。
12:00:00 222.123.123.25看了一次 计数
通过服务器对象application也可以
------解决方案--------------------
一个用户看过的文章的ID,可以放一个Session变量中
刷新计数时,检查这个用户是否看过。
------解决方案--------------------
防刷新的话用ajax了
------解决方案--------------------
------解决方案--------------------
Global.asax
- C# code
void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 int count = 0; int day_count = 0; int year = 0; int month = 0; int day = 0; DateTime today = DateTime.Now; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lianxiConnectionString"].ConnectionString); conn.Open(); SqlDataAdapter sda1 = new SqlDataAdapter("select [count] from [webcount]",conn); DataSet ds = new DataSet(); sda1.Fill(ds); //取总访问数 if (ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; count = Convert.ToInt32(dr["count"].ToString()); } ds.Clear(); SqlDataAdapter sda2 = new SqlDataAdapter("select * from [day_count]",conn); sda2.Fill(ds); //取每天访问数 if (ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; day_count = Convert.ToInt32(dr["day_count"].ToString()); year = Convert.ToInt32(dr["year"].ToString()); month = Convert.ToInt32(dr["month"].ToString()); day = Convert.ToInt32(dr["day"].ToString()); } //日期不同,则day_count为0 if (today.Year != year || today.Month != month || today.Day != day) day_count = 0; Application["count"] = count; Application["day_count"] = day_count; } void Application_End(object sender, EventArgs e) { // 在应用程序关闭时运行的代码 } void Application_Error(object sender, EventArgs e) { // 在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e) { // 在新会话启动时运行的代码 Application.Lock();//锁定Application int count = 0; int day_count = 0; int year = 0; int month = 0; int day = 0; DateTime today = DateTime.Now; year = today.Year; month = today.Month; day = today.Day; //访问次数都加1 count = (int)Application["count"]; day_count=(int)Application["day_count"]; count++; day_count++; Application["count"] = count; Application["day_count"] = day_count; //更新数据库 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lianxiConnectionString"].ConnectionString); conn.Open(); SqlCommand cmd1 = new SqlCommand("update [webcount] set [count]='"+count+"'",conn); cmd1.ExecuteNonQuery(); SqlCommand cmd2 = new SqlCommand("update [day_count] set [day_count]='"+day_count+"',[year]='"+year+"',[month]='"+month+"',[day]='"+day+"'",conn); cmd2.ExecuteNonQuery(); Application.UnLock();//解锁 } void Session_End(object sender, EventArgs e) { // 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。 }