Asp.net2.0下利用Global.asax全局文件实现流量分析

一、预览效果:

二:建立数据库脚本:

CREATE TABLE [dbo].[Yp_VisitStat] (
    [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页面代码:

<table class="tableBorder" cellspacing="1" cellpadding="2" width="100%" border="0" style="text-align: center">
        <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>&nbsp;&nbsp;
            <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>&nbsp;&nbsp;
        <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>&nbsp;&nbsp;
        <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>&nbsp;&nbsp;
        <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>&nbsp;&nbsp;
        <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;
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代码:

<%@ Application Language="C#" %>
<%@ 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>

 

转载于:https://www.cnblogs.com/yishuangyan/p/4357776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值