前台代码:用于显示网站访问量
<%@ 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>
<table style="width: 269px; height: 75px">
<tr>
<td align="center" style="font-weight: bold;
font-size: 30px; width: 237px; color: lime; background-color: gray;">
网站访问量</td>
</tr>
<tr>
<td align="center" style="width: 237px; background-color: gray;">
你是第<asp:Label ID="onlineCount" runat="server" T
ext="" Width="62px"><%=Application["onlinecount"]%>
</asp:Label>位访问者
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Global.asax代码:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %> 注释:用了文本文件
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
int count = 0;
StreamReader sdr;
// 获取文件路径
string filePath = Server.MapPath("count.txt");
// 打开文件
sdr = File.OpenText(filePath);
// 读取文件
while(sdr.Peek()!=-1)
{
string str = sdr.ReadLine();
// 把字符串强制类型转换成整型数据
count = int.Parse(str);
}
sdr.Close();
object objcount = count;
Application["onlinecount"] = count;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
int Oncount = 0;
Oncount = (int)Application["onlinecount"];
string filepath = Server.MapPath("count");
StreamWriter swr = new StreamWriter(filepath,false);
swr .WriteLine (Oncount );
swr .Close ();
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
int Oncount = 0;
Oncount =(int) Application["onlinecount"];
Oncount += 1;
object Onobj = Oncount;
Application["onlinecount"] = Onobj;
//将数据记录回到文件中
string filepath = Server.MapPath("count.txt");
StreamWriter swr=new StreamWriter (filepath,false);
swr.WriteLine(Oncount);
swr.Close();
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
</script>
另外一种方式实现如下:
一、建立一个数据表IPStat用于存放用户信息
我在IPStat表中存放的用户信息只包括登录用户的 IP(IP_Address),IP来源(IP_Src)和登录时间(IP_DateTime),些表的信息本人只保存一天的信息,如果要统计每个月的信息则要保存一个月。因为我不太懂对数据日志的操作,所以创建此表,所以说我笨吧,哈哈。
二、在Global.asax中获取用户信息
在Global.asax的Session_Start即新会话启用时获取有关的信息,同时在这里实现在线人数、访问总人数的增量统计,代码如下:
void Session_Start(object sender, EventArgs e)
{
//获取访问者的IP
string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
//获取访问者的来源
string ipSrc;
//判断是否从搜索引擎导航过来的
if (Request.UrlReferrer == null)
{
ipSrc = "";
}
else
{
//获取来源地址
ipSrc = Request.UrlReferrer.ToString();
}
//获取访问时间
DateTime ipDatetime = DateTime.Now;
//保存IP信息到数据库中
IPControl cont = new IPControl();
cont.AddIP(ipAddress, ipSrc, ipDatetime);
//获取用户访问的页面
string pageurl = Request.Url.ToString();
//判断访问的是否是默认页
if (pageurl.EndsWith("IPStat.aspx"))
{
//锁定变量
Application.Lock();
//为页面访问量+1
Application["StatCount"] = int.Parse(Application["StatCount"].ToString()) + 1;
//解锁
Application.UnLock();
}
//锁定变量
Session.Timeout = 10; //设定超时为10分钟
Application.Lock();
Application["countSession"] = Convert.ToInt32(Application["countSession"]) + 1; //访问总人数+1
Application["onlineWhx"] = (int)Application["onlineWhx"] + 1; //在线人数加+1
Session["login_name"] = null;
//解锁
Application.UnLock();
}
提醒一句,别忘了下面的代码,以实现在用户离线时,将在线人数减去1.
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
//锁定变量
Application.Lock();
Application["onlineWhx"] = (int)Application["onlineWhx"] – 1; //在线人数减-1
Session["login_name"] = null;
//解锁
Application.UnLock();
}
三、将以上有关信息保存到数据库IPStat
创建了一个获取IP数据信息的类IPControl(),用来实现对数据库IPStat数据的操作,关于IPControl()类的内容,因为它是C#中对数据库的操作,以解Sql server 数据库,就能看懂它, 这里就不作介绍了,请点击该链接查看。
为了实现将用户IP信息存入数据库,在上面代码中对IPControl()进行调用
//保存IP信息到数据库中
IPControl cont = new IPControl();
cont.AddIP(ipAddress, ipSrc, ipDatetime);
参数ipAddress为用户IP,ipSrc为用户来源, ipDatetime为用户进入时间。
四、创建定时器,定时操作有关数据
对以上IPSta数据库的数据,需要创建一个或者几个定时器,并在每天晚上24时前的10秒钟内统计一天的流量,然后将其删除,把统计结果保存到另一个数据表中,供页面显示昨日访问量是调用。定时器的创建和使用请点击创建一个或者几个定时器,供你参考。