<?php $br = php_sapi_name() == "cli" ? "" : "<br>"; if (!extension_loaded('qqwry')) { dl('qqwry.' . PHP_SHLIB_SUFFIX); } $qqwry = new qqwry('/Users/surfchen/projects/qqwry/nali/php/QQWry.Dat'); list($addr1, $addr2) = $qqwry->q('127.0.0.1'); $addr1 = iconv('GB2312', 'UTF-8', $addr1); $addr2 = iconv('GB2312', 'UTF-8', $addr2); echo $addr1, '|', $addr2, "\n"; $arr = $qqwry->q('222.216.47.4'); $arr[0] = iconv('GB2312', 'UTF-8', $arr[0]); $arr[1] = iconv('GB2312', 'UTF-8', $arr[1]); echo $arr[0], '|', $arr[1], "\n"; $arr = $qqwry->q('64.233.187.99'); $arr[0] = iconv('GB2312', 'UTF-8', $arr[0]); $arr[1] = iconv('GB2312', 'UTF-8', $arr[1]); echo $arr[0], '|', $arr[1], "\n";
/** * 获取ip对应的地区 * @param $ip ip */ public static function get($ip) { $ip = self::IpToInt($ip); if (self::$fp == NULL) { die('can not find qqwry.dat...'); } fseek(self::$fp, 0, SEEK_SET); $buf = fread(self::$fp, 8); self::$FirstStartIp = ord($buf[0]) + ord($buf[1]) * 256 + ord($buf[2]) * 256 * 256 + ord($buf[3]) * 256 * 256 * 256; self::$LastStartIp = ord($buf[4]) + ord($buf[5]) * 256 + ord($buf[6]) * 256 * 256 + ord($buf[7]) * 256 * 256 * 256; $RecordCount = floor((self::$LastStartIp - self::$FirstStartIp) / 7); if ($RecordCount <= 1) { self::$Country = 'FileDataError'; fclose(self::$fp); } $RangB = 0; $RangE = $RecordCount; while ($RangB < $RangE - 1) { $RecNo = floor(($RangB + $RangE) / 2); self::getStartIp($RecNo); if ($ip == self::$StartIp) { $RangB = $RecNo; break; } if ($ip > self::$StartIp) { $RangB = $RecNo; } else { $RangE = $RecNo; } } self::getStartIp($RangB); self::getEndIp(); if (self::$StartIp <= $ip && self::$EndIp >= $ip) { self::getCountry(); self::$Local = str_replace('我们一定要解放台湾!!!', '', self::$Local); self::$Local = str_replace('CZ88.NET', '', self::$Local); } else { self::$Country = '未知'; self::$Local = ''; self::$Local = str_replace('CZ88.NET', '', self::$Local); } fclose(self::$fp); return self::$Country . self::$Local; }
} }, groups : { phone:'mob_phone tel_phone' } }); }); var province=''; var city=''; var district=''; <?php /** * 根据用户ip自动默认选择省市* */ if (extension_loaded('qqwry')) { $q = new qqwry('/home/wwwroot/qqwry.dat'); $addres = $q->q(getenv("REMOTE_ADDR")); $addres[0] = iconv('GBK', "UTF-8//IGNORE", $addres[0]); if (mb_strpos($addres[0], '省') !== false) { echo "province='" . strstr($addres[0], '省', true) . "省';"; $c = strstr($addres[0], '省'); if (strpos($c, '市') !== false) { $d = mb_substr(strstr($c, '市'), 1); echo "city='" . mb_substr(strstr($c, '市', true), 1) . "市';"; } } elseif (mb_strpos($addres[0], '北京') !== false || mb_strpos($addres[0], '上海') !== false || mb_strpos($addres[0], '天津') !== false || mb_strpos($addres[0], '重庆') !== false) { echo "province='" . mb_substr($addres[0], 1) . "';"; echo "city='" . mb_substr($addres[0], 1) . "市';"; } elseif (mb_strpos($addres[0], '宁夏') !== false || mb_strpos($addres[0], '西藏') !== false || mb_strpos($addres[0], '新疆') !== false || mb_strpos($addres[0], '广西') !== false || mb_strpos($addres[0], '内蒙古') !== false) { switch (mb_substr($addres[0], 0, 2)) { case '广西':
if ($_SERVER['argc'] < 3) { usage(); } define('QQWRY_PATH', $_SERVER['argv'][1]); if (!file_exists($_SERVER['argv'][1])) { usage(); } //tow implementations in native php language, //used them to compare and test extension require $_SERVER['argv'][2]; $arr = array(); for ($i = 0; $i < 500; $i++) { $arr[] = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255); } $qqwry = new qqwry(QQWRY_PATH); $coolcode = new IpLocation(QQWRY_PATH); initConvertIp(QQWRY_PATH); echo "效率测试开始 ... "; $times = array(); $t = microtime(true); foreach ($arr as $ip) { convertip($ip); } $times[0] = microtime(true) - $t; $t = microtime(true); foreach ($arr as $ip) { $coolcode->getlocation($ip); } $times[1] = microtime(true) - $t; $t = microtime(true);