龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

asp.net实现网站在线人数统计实例

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
网上有好多PHP、ASP等的在线人数统计程序,这里介绍一个用ASP.NET来实现网站在线人数统计,主要是利用Global.asax全局文件来实现,挺简单,来看下吧。 首先在项目中选择“添加新项”,

网上有好多PHP、ASP等的在线人数统计程序,这里介绍一个用ASP.NET来实现网站在线人数统计,主要是利用Global.asax全局文件来实现,挺简单,来看下吧。

首先在项目中选择“添加新项”,添加“Global.asax”全局变量文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO ;

namespace movie
{
 /// <summary>
 /// Global 的摘要说明。
 /// </summary>
 public class Global : System.Web.HttpApplication
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.IContainer components = null;

  public Global()
  {
   InitializeComponent();
  }

  protected void Application_Start(Object sender, EventArgs e)
  {
   Application["conn"]="Server=localhost;database=movie;uid=sa;pwd='zcc';";
   Application["user_sessions"] = 0;
   Application["counter_num"]=0;


   uint count=0;
   StreamReader srd;
   //取得文件的实际路径
   string file_path=Server.MapPath ("counter.txt");
   //打开文件进行读取
   srd=File.OpenText (file_path);
   while(srd.Peek ()!=-1)
   {
    string str=srd.ReadLine ();
    count=UInt32.Parse (str);
   }
   object obj=count;
   Application["counter"]=obj;
   srd.Close ();
  }

  protected void Session_Start(Object sender, EventArgs e)
  {
   Application.Lock();
   Application["user_sessions"] = (int)Application["user_sessions"] + 1;
   Application.UnLock();

   Application.Lock ();
   //数值累加,注意这里使用了装箱(boxing)
   uint jishu=0;
   jishu=(uint)Application["counter"];
   jishu=jishu+1;
   object obj=jishu;
   Application["counter"]=obj;
   //将数据记录写入文件
   string file_path=Server.MapPath ("counter.txt");
   StreamWriter fs=new StreamWriter(file_path,false);
   fs.WriteLine (jishu);
   fs.Close ();
   Application.UnLock ();
  }

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {
  // Application.Lock();
  // Application["counter_num"]=(int)Application["counter_num"]+1;
  // Application.UnLock();
  }

  protected void Application_EndRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_Error(Object sender, EventArgs e)
  {

  }

  protected void Session_End(Object sender, EventArgs e)
  {
   Application.Lock();
   Application["user_sessions"] = (int)Application["user_sessions"] - 1;
   Application.UnLock();
  }

  protected void Application_End(Object sender, EventArgs e)
  {
   uint js=0;
   js=(uint)Application["counter"];
   //object obj=js;
   //Application["counter"]=js;
   //将数据记录写入文件
   string file_path=Server.MapPath ("counter.txt");
   StreamWriter fs=new StreamWriter(file_path,false);
   fs.WriteLine(js);
   fs.Close ();
  }

  #region Web 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
  }
  #endregion
 }
}

精彩图集

赞助商链接