使用PHP+纯真IP数据库将IP地址转为地区名

凉白开 PHP使用PHP+纯真IP数据库将IP地址转为地区名已关闭评论22,815字数 3570阅读11分54秒阅读模式

最近公司的评论系统被刷的比较厉害,今天下决心把过滤搞定,在做的过程中顺便使用php+纯真IP数据库将ip地址转地区名作为用户名.比如我是福建省福州市的网友,那我评论的默认用户名是"ttlsa福建省福州市网友",这样多少可以增加更省市的互动,而且至少看着舒服点.下面来讲讲具体的做法。
下载纯真IP数据库

,将qqwry.dat解压到站点目录下文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

    # wget http://6.scdx3.crsky.com/201307/qqwry0715.zip
    # unzip qqwry0715.zip
    # mv ip/qqwry.dat /data/site/test.ttlsa.com/qqwry.dat

    创建ip地址转地区名的PHP类:
ConvertIpNew.php文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

    <?php
        class cls_ipAddress{
            private $fp;
            private $firstip;
            private $lastip;
            private $totalip;

            public function __construct($filename="qqwry.dat"){
                $this->fp=0;
                if(($this->fp=@fopen($filename,"rb"))!==false){
                    $this->firstip=$this->getlong();
                    $this->lastip=$this->getlong();
                    $this->totalip=($this->lastip-$this->firstip)/7;
                    register_shutdown_function(array(&$this,"__destruct"));
                }
            }

            public function __destruct(){
                if($this->fp){
                    @fclose($this->fp);
                }
                $this->fp=0;
            }

            private function getlong(){
                $result=unpack("Vlong",fread($this->fp,4));
                return $result["long"];
            }

            private function getlong3(){
                $result=unpack("Vlong",fread($this->fp,3).chr(0));
                return $result["long"];
            }

            private function packip($ip){
                return pack("N",intval(ip2long($ip)));
            }

            private function getstring($data=""){
                $char=fread($this->fp,1);
                while(ord($char)>0){
                    $data.=$char;
                    $char=fread($this->fp,1);
                }
                return $data;
            }

            private function getarea(){
                $byte=fread($this->fp,1);
                switch(ord($byte)){
                case 0:
                    $operators="";
                break;
                case 1:
                case 2:
                    fseek($this->fp,$this->getlong3());
                    $operators=$this->getstring();
                break;
                default:
                    $operators=$this->getstring($byte);
                break;}
                return $operators;
            }

            public function getlocation($ip){
                if(!$this->fp){return null;}
                $location["ip"]=gethostbyname($ip);
                $ip=$this->packip($location["ip"]);
                $l=0;
                $u=$this->totalip;
                $findip=$this->lastip;
                while($l<=$u){
                    $i=floor(($l+$u)/2);
                    fseek($this->fp,$this->firstip+$i*7);
                    $startip=strrev(fread($this->fp,4));
                    if($ip<$startip){
                        $u=$i-1;
                    }else{
                        fseek($this->fp,$this->getlong3());
                        $endip=strrev(fread($this->fp,4));
                        if($ip>$endip){
                            $l=$i+1;
                        }else{
                            $findip=$this->firstip+$i*7;
                            break;
                        }
                    }
                }
                fseek($this->fp,$findip);
                $location["startip"]=long2ip($this->getlong());
                $offset=$this->getlong3();
                fseek($this->fp,$offset);
                $location["endip"]=long2ip($this->getlong());
                $byte=fread($this->fp,1);
                switch(ord($byte)){
                case 1:
                    $countryOffset=$this->getlong3();
                    fseek($this->fp,$countryOffset);
                    $byte=fread($this->fp,1);
                    switch(ord($byte)){
                    case 2:
                        fseek($this->fp,$this->getlong3());
                        $location["area"]=$this->getstring();
                        fseek($this->fp,$countryOffset+4);
                        $location["operators"]=$this->getarea();
                    break;
                    default:
                        $location["area"]=$this->getstring($byte);
                        $location["operators"]=$this->getarea();
                    break;}
                break;
                case 2:
                    fseek($this->fp,$this->getlong3());
                    $location["area"]=$this->getstring();
                    fseek($this->fp,$offset+8);
                    $location["operators"]=$this->getarea();
                break;
                default:
                    $location["area"]=$this->getstring($byte);
                    $location["operators"]=$this->getarea();
                break;}
                if($location["area"]=="CZ88.NET"){
                    $location["area"]="未知";
                }
                if($location["operators"]=="CZ88.NET"){
                    $location["operators"]="未知";
                }
                return $location;
            }
        }
?>

测试文件getArea.php文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

<?
        $username="ttlsa网友";
        $ip='120.33.31.22';
        if(!preg_match("/^[\d]+\.[\d]+\.[\d]+\.[\d]+$/isU",$ip)){
                $username = 'IP地址错误';
        }else{
                require_once './ConvertIpNew.php';
                $setip=new cls_ipAddress("./qqwry.dat");
                $location=$setip->getlocation($ip);
                $str=$location['area'];
                $str=iconv("gb2312", "utf-8//IGNORE",$str); //这边纯真IP数据库获取到的gb2312格式的文字,要先转成UTF8
                preg_match_all('/(.*?)省(.*?)市/',$str,$userLocation);
                if (is_array($userLocation)) {
                        $province = $userLocation[1][0];
                        $city = $userLocation[1][0];
                }
                $username = "ttlsa" . $province."省". $city . '市网友';
        }
}
?>

如下图,我们可以看到ttlsa福建省福州市网友。文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

IP转市区

使用php+纯真IP数据库将ip地址转地区名作为用户名

转载请注明出处:https://www.ttlsa.com/html/1766.html文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

 文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/

文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/文章源自运维生存时间-https://www.ttlsa.com/php/php_cunzhen-ipdata/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 26/07/2013 00:36:20
  • 转载请务必保留本文链接:https://www.ttlsa.com/php/php_cunzhen-ipdata/