一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php获取ip地址详细信息

时间:2010-07-16 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码

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();

热门栏目