add geo ip

This commit is contained in:
tianyu 2015-07-15 09:06:49 +08:00
parent ae00abcdb2
commit c354ccfdef
6 changed files with 273 additions and 0 deletions

BIN
ip/17monipdb.dat Normal file

Binary file not shown.

109
ip/IP.class.php Normal file
View File

@ -0,0 +1,109 @@
<?php
/*
全球 IPv4 地址归属地数据库(17MON.CN )
高春辉(pAUL gAO) <gaochunhui@gmail.com>
Build 20141009 版权所有 17MON.CN
(C) 2006 - 2014 保留所有权利
请注意及时更新 IP 数据库版本
数据问题请加 QQ : 346280296
Code for PHP 5.3+ only
*/
class IP
{
private static $ip = NULL;
private static $fp = NULL;
private static $offset = NULL;
private static $index = NULL;
private static $cached = array();
public static function find($ip)
{
if (empty($ip) === TRUE)
{
return 'N/A';
}
$nip = gethostbyname($ip);
$ipdot = explode('.', $nip);
if ($ipdot[0] < 0 || $ipdot[0] > 255 || count($ipdot) !== 4)
{
return 'N/A';
}
if (isset(self::$cached[$nip]) === TRUE)
{
return self::$cached[$nip];
}
if (self::$fp === NULL)
{
self::init();
}
$nip2 = pack('N', ip2long($nip));
$tmp_offset = (int)$ipdot[0] * 4;
$start = unpack('Vlen', self::$index[$tmp_offset] . self::$index[$tmp_offset + 1] . self::$index[$tmp_offset + 2] . self::$index[$tmp_offset + 3]);
$index_offset = $index_length = NULL;
$max_comp_len = self::$offset['len'] - 1024 - 4;
for ($start = $start['len'] * 8 + 1024; $start < $max_comp_len; $start += 8)
{
if (self::$index{$start} . self::$index{$start + 1} . self::$index{$start + 2} . self::$index{$start + 3} >= $nip2)
{
$index_offset = unpack('Vlen', self::$index{$start + 4} . self::$index{$start + 5} . self::$index{$start + 6} . "\x0");
$index_length = unpack('Clen', self::$index{$start + 7});
break;
}
}
if ($index_offset === NULL)
{
return 'N/A';
}
fseek(self::$fp, self::$offset['len'] + $index_offset['len'] - 1024);
self::$cached[$nip] = explode("\t", fread(self::$fp, $index_length['len']));
return self::$cached[$nip];
}
private static function init()
{
if (self::$fp === NULL)
{
self::$ip = new self();
self::$fp = fopen(__DIR__ . '/17monipdb.dat', 'rb');
if (self::$fp === FALSE)
{
throw new Exception('Invalid 17monipdb.dat file!');
}
self::$offset = unpack('Nlen', fread(self::$fp, 4));
if (self::$offset['len'] < 4)
{
throw new Exception('Invalid 17monipdb.dat file!');
}
self::$index = fread(self::$fp, self::$offset['len'] - 4);
}
}
public function __destruct()
{
if (self::$fp !== NULL)
{
fclose(self::$fp);
}
}
}
?>

83
ip/dbip-client.class.php Normal file
View File

@ -0,0 +1,83 @@
<?php
/**
*
* DB-IP.com API client class
*
* Copyright (C) 2012 db-ip.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
class DBIP_Client_Exception extends Exception {
}
class DBIP_Client {
private $base_url = "http://api.db-ip.com/";
private $api_key;
public function __construct($api_key, $base_url = null) {
$this->api_key = $api_key;
if (isset($base_url)) {
$this->base_url = $base_url;
}
}
protected function Do_API_Call($method, $params = array()) {
$qp = array("api_key=" . $this->api_key);
foreach ($params as $k => $v) {
$qp[] = $k . "=" . urlencode($v);
}
$url = $this->base_url . $method . "?" . implode("&", $qp);
if (!$jdata = @file_get_contents($url)) {
throw new DBIP_Client_Exception("{$method}: unable to fetch URL: {$url}");
}
if (!$data = @json_decode($jdata)) {
throw new DBIP_Client_Exception("{$method}: error decoding server response");
}
if (property_exists($data, 'error') && $data->error) {
throw new DBIP_Client_Exception("{$method}: server reported an error: {$data->error}");
}
return $data;
}
public function Get_Address_Info($addr) {
return $this->Do_API_Call("addrinfo", array("addr" => $addr));
}
public function Get_Key_Info() {
return $this->Do_API_Call("keyinfo");
}
}
?>

31
ip/example.php Executable file
View File

@ -0,0 +1,31 @@
#!/usr/local/bin/php
<?php
$api_key = "INSERT_YOUR_OWN_API_KEY_HERE";
require "dbip-client.class.php";
$ip_addr = $argv[1]
or die("usage: {$argv[0]} <ip_address>\n");
try {
$dbip = new DBIP_Client($api_key);
echo "keyinfo:\n";
foreach ($dbip->Get_Key_Info() as $k => $v) {
echo "{$k}: {$v}\n";
}
echo "\naddrinfo:\n";
foreach ($dbip->Get_Address_Info($ip_addr) as $k => $v) {
echo "{$k}: {$v}\n";
}
} catch (Exception $e) {
die("error: {$e->getMessage()}\n");
}
?>

43
ip/index.php Normal file
View File

@ -0,0 +1,43 @@
<?php
require "dbip-client.class.php";
require "IP.class.php";
$api_key = "YOUR_DB_IP_KEY(https://db-ip.com/api/)";
if (isset($_POST['geo']))
{
$ip_addr = $_POST['geo'];
}
else
{
$ip_addr = null;
}
$ip=$_SERVER['REMOTE_ADDR'];
if (empty($ip_addr)) {
echo $ip;
} else {
$dbip = new DBIP_Client($api_key);
foreach ($dbip->Get_Address_Info($ip_addr) as $k => $v) {
# echo "$k:$v,";
if ($k=="country" && $v=="CN") {
$finds = IP::find($ip_addr);
$result = "";
foreach ($finds as $res) {
if (!empty($res)) {
if ($result!=$res)
$result = empty($result)?$res:"$result,"."$res";
}
}
echo $result;
break;
}
if ($k=="address") {
echo "";
} else if ($k=="city") {
echo "$v";
} else {
echo "$v,";
}
}
}
?>

7
ip/index2.php Normal file
View File

@ -0,0 +1,7 @@
<?php
$api_key = "YOUR_DB_IP_KEY(https://db-ip.com/api/)";
require "dbip-client.class.php";
$ip=$_SERVER["REMOTE_ADDR"];
echo $ip;
?>