网站流量统计
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>网站浏览统计</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="count" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
注:label标签是用来展示网站统计流量的位置的,这个你可以随意放置哦!呵呵
后台代码:
long counter = 1;//声明一个计数器变量,并赋初值为1;
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
Application.Lock();//进行锁定,防止非同步更新
string countfile = Server.MapPath("count.txt");
if (File.Exists(countfile))
{
StreamReader sReader = new StreamReader(countfile);
string s;
long x;
s = sReader.ReadLine();
if (s != null)
{
x = Convert.ToInt64(s);
counter = x + 1;
}
sReader.Close();
}//end if
StreamWriter sWriter = new StreamWriter(countfile);
sWriter.Write((counter.ToString()));//将long类型转换为string类型
sWriter.Flush();
sWriter.Close();
Application.UnLock();
count.Text = "您是本站第" + counter + "位访客";
}
好了,自己测试一下吧!