萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php獲取ip地址詳細信息

php獲取ip地址詳細信息

php獲取ip地址詳細信息我們以前都是只提供簡單的如果getip一樣的函數去獲取用戶IP地址,下面這代碼是獲取用戶IP地址的詳細詳細信息,如:是來自哪裡。  代碼如下 復制代碼

function getip ()
        {
                if (getenv('http_client_ip')) {
                        $ip = getenv('http_client_ip');
                } else if (getenv('http_x_forwarded_for')) {
                        $ip = getenv('http_x_forwarded_for');
                } else if (getenv('remote_addr')) {
                        $ip = getenv('remote_addr');
                } else {
                        $ip = $_server['remote_addr'];
                }
                return $ip;
        }

 

function getipaddres ($ip = '')
        {
                if($ip == ''){
                        $ip = getip();
                }
                if($ip == '127.0.0.1'){
                        return $ip_addres = '本地機器';
                }
                $recordlength = 17+22+13+47+12+1;
                $ret = ereg('^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$', $ip, $ips教程ection);
                if($ret == false){$ip_addres = 'ip地址有錯';}
                for($i = 1; $i <= 4; $i ++){
                        if($ipsection[$i] > 255){
                                $ip_addres = 'ip地址有錯';
                        } else {
                                $thisip .= sprintf('%03.0f', $ipsection[$i]). (($i<4) ? '.' : '');
                        }
                }
                $fp= fopen($obj['rootdir'].'data/ip.txt', 'rb');
                if($fp == null){
                        $ip_addres = 'ip記錄文件打開出錯';
                }
                fseek($fp, 0, seek_end);
                $datafieldbegin = 0xc2;
                $recordcount = floor((ftell($fp)-$datafieldbegin)/$recordlength);
                if($recordcount <= 1){
                        $ip_addres = 'ip記錄文件打開出錯';
                } else {
                        $rangb = 0;
                        $range = $recordcount;
                        while($rangb<$range-1){
                                $recno = floor(($rangb+$range)/2);
                                fseek($fp,$recno*$recordlength+$datafieldbegin, seek_set);
                                $buf = fread($fp, $recordlength);
                                if(strlen($buf) == 0){
                                        return false;
                                }
                                $startip = (substr($buf, 0, 17));
                                $endip = trim(substr($buf, 17, 22));
                                $country = trim(substr($buf, 17+22, 13));
                                $local = trim(substr($buf, 17+22+13, 47));
                                if (strcmp($thisip, $startip) >= 0 && strcmp($thisip, $endip)<=0){
                                        break;
                                }
                                if(strcmp($thisip, $startip) > 0){
                                        $rangb = $recno;
                                } else {
                                        $range = $recno;
                                }
                        }
                        if(!($rangb < $range - 1)){
                                $ip_addres= '未知地址!';
                        } else {
                                $ip_addres = $country;
                                $ip_addres .= $local;
                        }
                }
                fclose($fp);
                return $ip_addres;
        }

// 調用 方法
echo getipaddres();

copyright © 萬盛學電腦網 all rights reserved