成长值: 246 签到天数: 4707 天 [LV.Master]伴坛终老
|
发表于 2016/2/8 05:26
|
显示全部楼层
|阅读模式
|Google Chrome 48.0.2564.103 |Windows 10
纯真
创建一个命名为cz88.class.php的文件,拉起代码例如
- <?
- /*
- $location = array($lang_functions['text_unknown'],"");
- 替换为
- $location = array(convertip($ip),"");
- */
- function convertip($ip) {
- //IP数据文件路径
- $dat_path = 'qqwry.dat';
- //检查IP地址
- if(!ereg("^([0-9]{1,3}.){3}[0-9]{1,3}$", $ip)){
- return ;
- }
- //打开IP数据文件
- if(!$fd = @fopen($dat_path, 'rb')){
- return;
- }
- //分解IP进行运算,得出整形数
- $ip = explode('.', $ip);
- $ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
- //获取IP数据索引开始和结束位置
- $DataBegin = fread($fd, 4);
- $DataEnd = fread($fd, 4);
- $ipbegin = implode('', unpack('L', $DataBegin));
- if($ipbegin < 0) $ipbegin += pow(2, 32);
- $ipend = implode('', unpack('L', $DataEnd));
- if($ipend < 0) $ipend += pow(2, 32);
- $ipAllNum = ($ipend - $ipbegin) / 7 + 1;
- $BeginNum = 0;
- $EndNum = $ipAllNum;
- //使用二分查找法从索引记录中搜索匹配的IP记录
- while($ip1num>$ipNum || $ip2num<$ipNum) {
- $Middle= intval(($EndNum + $BeginNum) / 2);
- //偏移指针到索引位置读取4个字节
- fseek($fd, $ipbegin + 7 * $Middle);
- $ipData1 = fread($fd, 4);
- if(strlen($ipData1) < 4) {
- fclose($fd);
- return;
- }
- //提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
- $ip1num = implode('', unpack('L', $ipData1));
- if($ip1num < 0) $ip1num += pow(2, 32);
- //提取的长整型数大于我们IP地址则修改结束位置进行下一次循环
- if($ip1num > $ipNum) {
- $EndNum = $Middle;
- continue;
- }
- //取完上一个索引后取下一个索引
- $DataSeek = fread($fd, 3);
- if(strlen($DataSeek) < 3) {
- fclose($fd);
- return;
- }
- $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
- fseek($fd, $DataSeek);
- $ipData2 = fread($fd, 4);
- if(strlen($ipData2) < 4) {
- fclose($fd);
- return;
- }
- $ip2num = implode('', unpack('L', $ipData2));
- if($ip2num < 0) $ip2num += pow(2, 32);
- //没找到提示未知
- if($ip2num < $ipNum) {
- if($Middle == $BeginNum) {
- fclose($fd);
- return;
- }
- $BeginNum = $Middle;
- }
- }
- //下面的代码读晕了,没读明白,有兴趣的慢慢读
- $ipFlag = fread($fd, 1);
- if($ipFlag == chr(1)) {
- $ipSeek = fread($fd, 3);
- if(strlen($ipSeek) < 3) {
- fclose($fd);
- return;
- }
- $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
- fseek($fd, $ipSeek);
- $ipFlag = fread($fd, 1);
- }
- if($ipFlag == chr(2)) {
- $AddrSeek = fread($fd, 3);
- if(strlen($AddrSeek) < 3) {
- fclose($fd);
- return;
- }
- $ipFlag = fread($fd, 1);
- if($ipFlag == chr(2)) {
- $AddrSeek2 = fread($fd, 3);
- if(strlen($AddrSeek2) < 3) {
- fclose($fd);
- return;
- }
- $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
- fseek($fd, $AddrSeek2);
- } else {
- fseek($fd, -1, SEEK_CUR);
- }
- while(($char = fread($fd, 1)) != chr(0))
- $ipAddr2 .= $char;
- $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
- fseek($fd, $AddrSeek);
- while(($char = fread($fd, 1)) != chr(0))
- $ipAddr1 .= $char;
- } else {
- fseek($fd, -1, SEEK_CUR);
- while(($char = fread($fd, 1)) != chr(0))
- $ipAddr1 .= $char;
- $ipFlag = fread($fd, 1);
- if($ipFlag == chr(2)) {
- $AddrSeek2 = fread($fd, 3);
- if(strlen($AddrSeek2) < 3) {
- fclose($fd);
- return;
- }
- $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
- fseek($fd, $AddrSeek2);
- } else {
- fseek($fd, -1, SEEK_CUR);
- }
- while(($char = fread($fd, 1)) != chr(0)){
- $ipAddr2 .= $char;
- }
- }
- fclose($fd);
- //最后做相应的替换操作后返回结果
-
- $ipAddr1=mb_convert_encoding($ipAddr1,"utf-8","gb2312".',auto');
- $ipAddr2=mb_convert_encoding($ipAddr2,"utf-8","gb2312".',auto');
-
- if(preg_match('/http/i', $ipAddr2)) {
- $ipAddr2 = '';
- }
-
- if(preg_match('/大学/i', $ipAddr1)) {
- $ipAddr2 = '';
- $ipAddr1 =preg_replace("/大学.*/is", "", $ipAddr1)."大学";
- }elseif(preg_match('/大学/i', $ipAddr2)) {
- $ipAddr1 = '';
- $ipAddr2 =preg_replace("/大学.*/is", "", $ipAddr2)."大学";
- }
-
- if(preg_match('/学院/i', $ipAddr1)) {
- $ipAddr2 = '';
- $ipAddr1 =preg_replace("/学院.*/is", "", $ipAddr1)."学院";
- }elseif(preg_match('/学院/i', $ipAddr2)) {
- $ipAddr1 = '';
- $ipAddr2 =preg_replace("/学院.*/is", "", $ipAddr2)."学院";
- }
-
-
-
-
- $ipaddr = str_replace(" ", "",$ipAddr1.$ipAddr2);
-
-
-
- $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
- $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
- $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
- if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
- return "IPV4";
- }
-
- return ($ipaddr);
- }
复制代码
使用打印
echo convertip('91.121.255.214');
//输出: 法国巴黎OVHSAS数据中心
实例
- <?php
- header('Content-Type: text/plain; charset=utf-8');
- include_once('cz88.class.php');
- $ip="";
- header('Content-Type: text/plain; charset=utf-8');
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
- $ip=$_SERVER['HTTP_CLIENT_IP'];
- } elseif (!empty($_SERVER['X-Real-IP'])) {
- $ip=$_SERVER['X-Real-IP'];
- } elseif (!empty($_SERVER['CF-Connecting-IP'])) {
- $ip=$_SERVER['CF-Connecting-IP'];
- } else {
- $ip=$_SERVER['REMOTE_ADDR'];
- }
- echo $ip=preg_replace("/^::ffff:/", "", $ip);
- echo "\n";
- echo '纯真IP 数据:';echo convertip($ip);
复制代码
ipip
php使用方法例如,参考,注意文件编码utf-8,ipip安装包已经自带IP.class.php解析文件,引用即可
- <?php
- header('Content-Type: text/plain; charset=utf-8');
- include_once('IP.class.php');
- print_r(IP::find('119.29.29.29'));
- print_r(IP::find($_SERVER['REMOTE_ADDR']));
- echo 'IPIP.net 数据:'.join('',IP::find($_SERVER['REMOTE_ADDR']));
复制代码
实例
- <?php
- header('Content-Type: text/plain; charset=utf-8');
- include_once('IP.class.php');
- $ip="";
- header('Content-Type: text/plain; charset=utf-8');
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
- $ip=$_SERVER['HTTP_CLIENT_IP'];
- } elseif (!empty($_SERVER['X-Real-IP'])) {
- $ip=$_SERVER['X-Real-IP'];
- } elseif (!empty($_SERVER['CF-Connecting-IP'])) {
- $ip=$_SERVER['CF-Connecting-IP'];
- } else {
- $ip=$_SERVER['REMOTE_ADDR'];
- }
- echo "您当前的IP:";
- echo $ip=preg_replace("/^::ffff:/", "", $ip);
- echo "\n";
- echo 'IPIP.net 数据:'.join('',IP::find($ip));
复制代码
|
|