网站浏览总次数次数和最高在线人数怎么做
最好是绑定数据库的方法
网站浏览总次数次数和最高在线人数
建立一个Global.asax里面代码如下:<%@ Application Language="C#" %>
<%@ Import Namespace=" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
Application["UsersOnline"] = 0;
int 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 = Int32.Parse(str);
}
object obj = count;
Application["TotalCount"] = obj;
srd.Close();
}
void Application_End(object sender, EventArgs e)
{
int count = 0;
count = (int)Application["TotalCount"];
string file_path = Server.MapPath("counter.txt");
StreamWriter fs = new StreamWriter(file_path, false);
fs.WriteLine(count);
fs.Close();
//DataSet objDataSet = new DataSet();
//objDataSet.ReadXml(Server.MapPath("count.xml"));
//int maxCount = Convert.ToInt32(objDataSet.Tables["MyCount"].Rows[0]["MaxCount"]);
//Application["MaxCount"] = maxCount;
//if (maxCount < (int)Application["UsersOnline"]) //如果MaxCount标签里面的值小于当前在线人数的值,就存入
// objDataSet.Tables["MyCount"].Rows[0]["MaxCount"] = (int)Application["UsersOnline"];
//objDataSet.WriteXml(Server.MapPath("count.xml"));
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
StreamReader srd;
int maxCount = 0;
//取得文件的实际路径
string file_pathm = Server.MapPath("max.txt");
//打开文件进行读取
srd = File.OpenText(file_pathm);
while (srd.Peek() != -1)
{
string str = srd.ReadLine();
maxCount = Int32.Parse(str);
}
srd.Close();
if (maxCount < (int)Application["UsersOnline"])
maxCount = (int)Application["UsersOnline"];
string max_file_path = Server.MapPath("max.txt");
StreamWriter max_fs = new StreamWriter(max_file_path, false);
max_fs.WriteLine(maxCount);
max_fs.Close();
//访问总数加1
int count = 0;
count = (int)Application["TotalCount"];
count = count + 1;
Application["TotalCount"] = count;
//将数据记录写入文件
string file_path = Server.MapPath("counter.txt");
try
{
StreamWriter fs = new StreamWriter(file_path, false);
fs.WriteLine(count);
fs.Close();
}
catch (Exception ex)
{
Application["Error"] = ex.Message;
}
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;
Application.UnLock();
}
</script>
使用时:
protected void Page_Load(object sender, EventArgs e)
{
if (Application["UsersOnline"] != null)
{
currentUserNum.Text = Application["UsersOnline"].ToString();
}
if (Application["TotalCount"] != null)
{
totalUserNum.Text = Application["TotalCount"].ToString();
}
if (Application["UsersOnline"] != null)
{
StreamReader srd;
int maxCount = 0;
//取得文件的实际路径
string file_path = Server.MapPath("max.txt");
//打开文件进行读取
srd = File.OpenText(file_path);
while (srd.Peek() != -1)
{
string str = srd.ReadLine();
maxCount = Int32.Parse(str);
}
srd.Close();
if (maxCount < int.Parse(Application["UsersOnline"].ToString()))
maxCount = int.Parse(Application["UsersOnline"].ToString());
maxUserNum.Text = maxCount.ToString();
}
根据需要建txt文件