通过Global.asax和物理文件记录访问用户数

2017-02-11 20:19:49  访问(1327) 赞(0) 踩(0)

  • <%@ Application Language="C#" %>
    
    <script runat="server">
    
        void Application_Start(object sender, EventArgs e)
        {
            
            try
            {
    
    
                GlobalData.Application_Start();
            }
            catch(Exception err)
            {
                SaveError.i = new SaveError(err);
            }
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //  Code that runs on application shutdown
    
        }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // Code that runs when an unhandled error occurs
    
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Session_Start(object sender, EventArgs e) 
        {
            try
            {
                GlobalData.Session_Start();
            }
            catch
            {
            }
        }
    
        /// <summary>
        /// Session结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Session_End(object sender, EventArgs e) 
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
    
        }
           
    </script>
    
    

  • using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    
    /// <summary>
    /// 全局数据
    /// </summary>
    public class GlobalData
    {
        public GlobalData()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    
        /// <summary>
        /// 
        /// </summary>
        private static bool m_isLoad = false;
    
        /// <summary>
        /// 
        /// </summary>
        private static ulong m_StatCount = 0;
    
        /// <summary>
        /// 
        /// </summary>
        public static ulong StatCount
        {
            get
            {
                return m_StatCount;
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static string GetFileName()
        {
            if (HttpContext.Current == null)
                return "";
            
            return HttpContext.Current.Request.PhysicalApplicationPath + "visitlog/StatCount.txt";
    
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ReadFile(string str)
        {
            try
            {
                if (str == null || str.Length == 0 || !File.Exists(str))
                    return "";
    
                return File.ReadAllText(str);
            }
            catch
            {
                return "";
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="theValue"></param>
        private static void WriteFile(string str, ulong theValue)
        {
            try
            {
                if (str == null || str.Length == 0)
                    return;
    
                if (File.Exists(str))
                {
                    File.Delete(str);
                    File.WriteAllText(str, theValue.ToString());
    
                    return;
                }
    
                FileInfo fi = new FileInfo(str);
    
                if (!fi.Directory.Exists)
                    fi.Directory.Create();
    
    
                File.WriteAllText(fi.FullName, theValue.ToString());
            }
            catch
            {
    
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        public static void Application_Start()
        {
            if (HttpContext.Current == null)
                return;
    
    
            try
            {
                if (!m_isLoad)
                {
                    m_isLoad = true;
    
                    string fileName = GetFileName();
    
                    if (!File.Exists(fileName))
                        return;
    
                    string fileValue = ReadFile(fileName);
    
                    if (fileValue != null && fileValue.Length > 0)
                    {
                        ulong uv = 0;
                        if (ulong.TryParse(fileValue, out uv))
                            m_StatCount = uv;
                    }
    
                }
     
            }
            catch
            {
    
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        public static void Session_Start()
        {
            if (HttpContext.Current == null)
                return;
    
    
            try
            {
                 
                //获取用户访问的页面
                string pageurl 
                    = 
                    HttpContext.Current.Request.Url.ToString();
    
                //判断访问的是否是默认页
                if (pageurl.ToLower().EndsWith("default.aspx"))
                {
                    m_StatCount += 1;
    
                    WriteFile(GetFileName(), m_StatCount);
                }
            }
            catch
            {
    
            }
        }
    }
    
    

标签:通过Global.asax和物理文件记录访问用户数 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)