问题:如何统计一个站点被访问的次数?
Application对象:应用程序级别的数据保存。
Application特征:位置:.服务器内存,执行速度快,使用范围:整个应用程序。类型:任意类型。生命周期:应用程序开始创建到销毁。
全局应用程序类:Global.asax :处理应用程序级事件的可选文件。必须放在在应用程序的根目录下。
Application_Start:接受第一个请求时触发、Application_End:应用程序结束时触发
Session_Start:某用户第一次访问时触发、Session_End:某用户退出应用程序时触发
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Application.Lock();
Application["UserVisit"] = 0;//网站被访问的次数
Application["CurrentUsers"] = 0;//在线人数
Application.UnLock();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application["UserVisit"] = (int)Application["UserVisit"] + 1; ;//网站被访问的次数
Application["CurrentUsers"] =(int) Application ["CurrentUsers"]+1;//在线人数
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不引发该事件。
Application.Lock();
Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
Application.UnLock();
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.lta.Text = "您是本网站第" + Application["UserVisit"].ToString() + "位访客" + "当前在线人数:" + Application["CurrentUsers"].ToString();
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
Session.Abandon();
}