zoukankan      html  css  js  c++  java
  • 网站来访者IP及地址记录

    将此段代码放入网站的母版页或者header的用户控件中。

            private void AccessRecord()
            {
                string ip = Request.UserHostAddress;
                string ipDataPath = Server.MapPath("QQWry.Dat");//纯真ip数据库http://www.cz88.net        
                Fireicesion.WebTools.IpAddressSearch ipSearch = new Fireicesion.WebTools.IpAddressSearch(ipDataPath);//前一篇随笔中的根据ip获取地址
                string address = ipSearch.GetAddressWithIP(ip);
                if (CheckRecord(ip))
                {
                    string sql = "insert into AccessRecord (ip,address,recordtime) values('" + ip + "','" + address + "','" + DateTime.Now.ToString() + "')";
                    DB.Insert(sql);
                }
            }
    
    
            /// <summary>
            /// 上次访问时间,true可记录,false不记录
            /// </summary>
            /// <param name="ip"></param>
            /// <returns></returns>
            private bool CheckRecord(string ip)
            {
                string sql = "select * from AccessRecord where ip='" + ip + "' order by RecordTime desc limit 0,1";
                DataTable dt = DB.GetDataTable(sql);
                if (dt.Rows.Count == 0)//没有找到记录,此IP未曾访问过
                    return true;
                else
                {
                    DateTime lastTime = Convert.ToDateTime(dt.Rows[0]["RecordTime"]);
                    DateTime now = DateTime.Now;
                    TimeSpan timeSpan = new TimeSpan(0, 30, 0);//半个小时内访问不重复记录
                    if (lastTime.Add(timeSpan).CompareTo(now) == -1)
                    {
                        return true;
                    }
                    return false;
                }
            }
    
    

    数据库表(MySql格式的)

    CREATE TABLE `accessrecord` (
      `Id` int(11) NOT NULL AUTO_INCREMENT,
      `Ip` varchar(80) DEFAULT NULL,
      `Address` varchar(100) DEFAULT NULL,
      `Recordtime` datetime DEFAULT '2000-01-01 00:00:00',
      PRIMARY KEY (`Id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8;
    
  • 相关阅读:
    dup/dup2函数
    read/write函数
    lseek函数
    流程控制
    vim普通模式
    vim实用技巧1
    python源代码解读
    python变量命名规则
    python之字符串2
    Docker系列文章
  • 原文地址:https://www.cnblogs.com/fireicesion/p/1781524.html
Copyright © 2011-2022 走看看