如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
来源:互联网 发布:网络消费陷阱揭秘 编辑:程序博客网 时间:2024/06/05 16:29
步骤1:新建一个Global.asax 全局应用程序类
<%@ Application Language="C#"%>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
long totalCount = 0; //总访问数
int todayCount = 0;//今天在线人数
//先从XML中取得访问数
string xmlFilePath = Server.MapPath("~/") + "xml\\count.xml"; //XML文件路径
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(xmlFilePath);
totalCount = Int64.Parse(ds.Tables[0].Rows[0]["TotalCount"].ToString().Trim());
//设置Application,设置在线数量,总访问数量
Application["total_count"] = totalCount;
Application["today_count"] = todayCount;
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();//锁定
long totalCount = 0; //总访问数
int todayCount = 0;//今天访问数
totalCount = Convert.ToInt64(Application["total_count"]);
todayCount = Convert.ToInt32(Application["today_count"]);
totalCount++;//用户登录,以后才修改访问总数
todayCount++;//在线用户数量加 1
Application["total_count"] = totalCount;
Application["today_count"] = todayCount;
//把访问数写如到XML文件中
string xmlFilePath = Server.MapPath("~/") + "xml/count.xml"; //XML文件路径
System.Xml.XmlDocument xmlObj = new System.Xml.XmlDocument();
xmlObj.Load(xmlFilePath);
System.Xml.XmlNode xmlRootOjb = xmlObj.SelectSingleNode("Web/AccessInfo/TotalCount");
xmlRootOjb.InnerText = totalCount.ToString();//增加总的访问量,并修改xml总访问量的值
xmlObj.Save(xmlFilePath);
Application.UnLock();//解锁
}
void Session_End(object sender, EventArgs e)
{ //这里主要计算登录用户
Application.Lock();
Application["today_count"] = Int32.Parse(Application["today_count"].ToString()) - 1;
//当用户离开站点的时候,在线总数减1
Application.UnLock();
}
</script>
步骤2:新建一个xml文档用来存放 在线人数
<?xml version="1.0" encoding="utf-8"?>
<Web>
<AccessInfo>
<TotalCount>4</TotalCount>
</AccessInfo>
</Web>
步骤3:在aspx中输出 统计的在线人数
Response.Write(Application["today_count"]);
经过我测试 这个还是不是很准确。 小网站凑合用吧
- 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- 网站访问量统计实现.
- js 实现统计网站访问量
- java实现网站访问量统计
- 如何实现ASP.NET中网站访问量的统计
- 如何实现ASP.NET中网站访问量的统计
- java jsp实现网站访问量的统计
- Servlet过滤器实现统计网站访问量
- 网站访问量的统计(Application和Session应用)
- 使用ServletContext对象统计网站的访问量
- 使用ServletContext对象统计网站的访问量
- dwr 统计网站访问量
- 网站访问量统计
- 网站访问量统计
- 统计网站访问量
- 网站访问量的统计
- java统计网站访问量
- servletContext
- VMware_Workstation_7.1.3_324285注册码
- Ext.form.ComboBox
- 彻底抛弃 ubuntu 10.04 中的 pulse(解决Skype中麦克风不工作问题)
- 按键中断
- 如何统计网站访问量,代码实现使用全局变量 Application,start,end,Session_start,Session_end事件
- 数据对齐与poppack.h~
- C++ 对象的内存布局(上)
- C++ 对象的内存布局(下)
- PLINQ
- oracle execute immediate
- undefined reference to `vtable for “classname”
- vs2008中新建项目后看不到解决方案的解决办法
- VMR9显示的图片颜色失真,花屏?