Asp.net2.0下利用Global.asax全局文件实现流量分析
一、预览效果:
二:建立数据库脚本:
[iVisitStat_ID] [int] IDENTITY (1, 1) NOT NULL ,
[sIntoIP] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[dIntoTime] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Yp_VisitStat] ADD
CONSTRAINT [PK_Yp_VisitStat] PRIMARY KEY CLUSTERED
(
[iVisitStat_ID]
) ON [PRIMARY]
GO
三、前台Aspx页面代码:
<tr>
<th style="HEIGHT: 21px" colspan="2" >
<span style="font-size: 10pt; color: #ffffff">网站综合流量分析</span></th>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">给合流量统计:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">上月流量统计:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">本月流量统计:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">昨日流量统计:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label10" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">今日流量统计:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label12" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="TableRow2" style="text-align: right; width: 126px;">当前在线状况:</td>
<td class="TableRow2" style="text-align: left;">
<asp:Label ID="Label13" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
四、后置代码类:
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
VisitStat();
}
}
private void VisitStat()//流量分析
{
string LastMonthBegin = DateTime.Today.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:01";//上个月的起始时间
string ThisMonthBegin = DateTime.Today.AddMonths(0).ToString("yyyy-MM") + "-01 00:00:01";//本个月的起始时间
string NextMonthBegin = DateTime.Today.AddMonths(1).ToString("yyyy-MM") + "-01 00:00:01";//下个月的起始时间
string YesterdayBegin = DateTime.Now.AddDays(-1).ToString("d") + " 00:00:01";//昨天的起始时间
string TodayBegin = DateTime.Now.AddDays(0).ToString("d") + " 00:00:01";//今天的起始时间
string TomorrowBegin = DateTime.Now.AddDays(1).ToString("d") + " 00:00:01";//明天的起始时间
Label3.Text = "累计PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat]");
Label4.Text = "累计独立IP浏览量:" + SqlResult("select distinct count(distinct sIntoIP) from [Yp_VisitStat]");
Label5.Text = "上个月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘");
Label6.Text = "上个月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘");
Label7.Text = "本月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘");
Label8.Text = "本月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘");
Label9.Text = "昨日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘");
Label10.Text = "昨日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘");
Label11.Text = "今日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘");
Label12.Text = "今日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘");
Label13.Text = "当前共有:<b>" + Application["counter"].ToString() + "</b> 位访问者正在访问网站 !";
}
public static string SqlResult(string MySql)//查询库并返回记录个数
{
string strResult = null;
SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString());
Conn.Open();
SqlCommand myCommand = new SqlCommand(MySql, Conn);
strResult = myCommand.ExecuteScalar().ToString();
Conn.Close();
Conn.Dispose();
return strResult;
}
五:Global.asax代码:
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Application.Lock();
Application["counter"] = 0;
Application.UnLock();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
Application.Lock();
Application["counter"] = (int)Application["counter"] - 1;
Application.UnLock();
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application["counter"] = (int)Application["counter"]+1;
string sIntoIP;
string dIntoTime;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
dIntoTime = DateTime.Now.ToString();
SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString());
Conn.Open();
SqlCommand myCommand = new SqlCommand("insert into [Yp_VisitStat] (sIntoIP,dIntoTime) values (‘" + sIntoIP + "‘,‘" + dIntoTime + "‘)", Conn);
myCommand.ExecuteNonQuery();
Conn.Close();
Conn.Dispose();
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
Application.Lock();
Application["counter"] = (int)Application["counter"] - 1;
Application.UnLock();
}
</script>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。