- 浏览: 13638657 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
来源: http://www.phpandstuff.com/articles/geoip-country-lookup-with-php
GeoIP + PHP
<?php //计时开始 function utime() { $time = explode( " ", microtime() ); $usec = (double)$time[0]; $sec = (double)$time[1]; return $usec + $sec; } $startTimes = utime(); // include the php script // wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz // gunzip GeoIP.dat.gz include("geoip.inc"); // open the geoip database $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD); // 获取国家代码 $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country code is: <strong>$country_code</strong> <br />"; // 获取国家名称 $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country name is: <strong>$country_name</strong> <br />"; // close the database geoip_close($gi); //运行结束时间 $endTimes = utime(); $runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) ); echo "Processed in " . $runTimes . "second."; ?>
注:在本地测试的话因 为$_SERVER['REMOTE_ADDR']和$_SERVER['REMOTE_ADDR']可能是127.0.0.1,所 以输出的内容为空。可以自己带入IP测试
或者 使用某网站的API
API 1.
1. 返回文字
http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true
2. 返回图片
<IMG SRC="http://api.hostip.info/flag.php?ip=12.215.42.19" ALT="IP Address Lookup">
API 2. (需要申请api key ,免费的,类似google)
城市:
http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=74.125.45.100
国家(更快) :
http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100
key | Yes | <empty> | API key provided with your free account. |
ip | No | Client IP | IP address |
format | No | raw | raw, xml, json |
callback | No | <empty> | Required when using json callback. |
- If you only need the country name, avoid using the city precision API.
- If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)
ip-city | City | Yes | Yes |
ip-country | Country | No | Yes |
- If you only need the country name, avoid using the city precision API.
- If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)
使用类:
<?php final class ip2location_lite{ protected $errors = array(); protected $service = 'api.ipinfodb.com'; protected $version = 'v3'; protected $apiKey = ''; public function __construct(){} public function __destruct(){} public function setKey($key){ if(!empty($key)) $this->apiKey = $key; } public function getError(){ return implode("\n", $this->errors); } public function getCountry($host){ return $this->getResult($host, 'ip-country'); } public function getCity($host){ return $this->getResult($host, 'ip-city'); } private function getResult($host, $name){ $ip = @gethostbyname($host); if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){ $xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml'); try{ $response = @new SimpleXMLElement($xml); foreach($response as $field=>$value){ $result[(string)$field] = (string)$value; } return $result; } catch(Exception $e){ $this->errors[] = $e->getMessage(); return; } } $this->errors[] = '"' . $host . '" is not a valid IP address or hostname.'; return; } } ?>
<? include('ip2locationlite.class.php'); //Load the class $ipLite = new ip2location_lite; $ipLite->setKey('<your_api_key>'); //Get errors and locations $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']); $errors = $ipLite->getError(); //Getting the result echo "<p>\n"; echo "<strong>First result</strong><br />\n"; if (!empty($locations) && is_array($locations)) { foreach ($locations as $field => $val) { echo $field . ' : ' . $val . "<br />\n"; } } echo "</p>\n"; //Show errors echo "<p>\n"; echo "<strong>Dump of all errors</strong><br />\n"; if (!empty($errors) && is_array($errors)) { foreach ($errors as $error) { echo var_dump($error) . "<br /><br />\n"; } } else { echo "No errors" . "<br />\n"; } echo "</p>\n";
数据库版:
IP geolocation databases download
Updated Mar 5 2011
DB1 | 1.60 | 123.123.123 | ISO country code, country name | CSV BIN |
DB3 | 17.11 | 123.123.123 | ISO country code, country name, state, city | CSV BIN |
DB5 | 21.40 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude | CSV BIN |
DB9 | 22.76 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes | CSV BIN |
DB11 | 23.28 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes, time zone | CSV BIN |
key: c9dcc88453e33a9e63ebad8d65f91583e87abd8185dd95f09fbeef6c62264f7d
其他参考
http://pecl.php.net/package/geoip
http://www.geoiptool.com/
http://www.hostip.info/use.html
http://phpweby.com/software/ip2country
http://www.ipinfodb.com/index.php
转帖注明出处:http://justcoding.iteye.com/blog/986355
本站链接:
php 显示ip所属地 (qq版)
- getip.zip (582.4 KB)
- 下载次数: 111
- ip2locationlite.zip (733 Bytes)
- 下载次数: 68
- phpwebyip2country.zip (2 MB)
- 下载次数: 94
发表评论
-
PHP: 在类(class)中加载动态函数, 变量函数或半变量函数 variable function/method
2016-09-03 07:54 7102最终实例在下方 以前 ... -
MySQL入门 (七) : 储存引擎与资料型态
2016-09-03 07:49 45031 表格与储存引擎 表格(table)是资料库中用来储存 ... -
MySQL入门 (六) : 字元集与资料库
2016-09-03 07:47 45091 Character Set与Collation 任何 ... -
MySQL入门 (五) : CRUD 与资料维护
2016-09-03 07:46 54231 取得表格资讯 1.1 DESCRIBE指令 「 ... -
MySQL入门 (四) : JOIN 与UNION 查询
2016-09-03 07:42 44431 使用多个表格 在「world」资料库的「countr ... -
PHP: 关键字global 和 超全局变量$GLOBALS的用法、解释、区别
2016-08-31 12:07 4982$GLOBALS 是一个关联数组,每一个变量为一个 ... -
MySQL入门 (三) : 运算式与函式
2016-08-31 12:01 4331运算式(expressions)已经 ... -
MySQL入门 (二) : SELECT 基础查询
2016-08-31 11:56 46371 查询资料前的基本概念 1.1 表格、纪录 ... -
MySQL入门 (一) : 资料库概论与MySQL的安装
2016-08-31 11:51 43961. 储存与管理资料 储存与管理资料一直是资讯应用上最基本 ... -
MySQL入门 (九) : 子查询 Subquery
2016-08-30 02:26 44001 一个叙述中的查询叙述 子查询(subquery)是一 ... -
PHP: 用readonly取代disabled来获取input值 submit a disabled input in a form could not ge
2016-08-30 02:21 2652The form is like below; <f ... -
PHP7革新与性能优化
2016-08-30 02:20 2098有幸参与2015年的PHP技 ... -
Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
2016-08-18 06:03 3070对于SQL的Join,在学习起来可能是比较乱的。我们知道, ... -
Comet 反Ajax: 基于jQuery与PHP实现Ajax长轮询(LongPoll)
2016-08-18 06:00 1330传统的AJAX轮询方式,客服端以用户定义的时间间隔去服务器上 ... -
PHP:ServerPush (Comet推送) 技术的探讨
2016-08-18 05:58 990PHP中Push(推送)技术的探讨 [http://vi ... -
PHP: 手把手编写自己的 MVC 框架实例教程
2016-08-16 05:33 17621 什么是MVC MVC模式(Model-View-Con ... -
PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
2016-07-29 12:55 1702原文: PHP5: mysqli 插入, 查询, 更新 ... -
MongoDB 教程索引 (附有视频)
2016-07-27 10:54 696MongoDB 教程索引 MongoDB 教程一: ... -
Node.js 模块之Nimble流程控制
2016-07-18 12:59 1039NodeJS异步的特性有时候 ... -
Node.js web应用模块之Supervisor
2016-07-18 12:56 2284在开发或调试Node.js应 ...
相关推荐
本代码包实现了两个功能: 1.集成GeoIP,通过ip查询国家信息 2.将查询到的国家编码对应到中文名
logstash使用geoip插件解析出来的地理信息是英文。通过GeoIP获取ip所属地,并将国家转换为中文。
PHP版基于GeoIP2实现根据ip获取地理位置 laravel php版本大于7.2
maxmind geoip国家库。nginx geoip模块依赖的country库。.mmd格式。根据ip获取国家编码,国家名称等信息。
Maxmind GeoIP2-Country-CSV_20200331 朋友买的收费库 通过GeoIP获取ip所属地 完整: GeoIP2-Anonymous-IP-CSV_20200428.zip GeoIP2-Anonymous-IP_20200428.tar.gz GeoLite2-ASN-CSV_20200428.zip GeoLite2-...
官网下载,最新GeoLite2/ GeoIP 全球国家IP定位离线库 2020.08.18 最新版 (mmdb)
GeoIP地址库,用于查找对应IP地址对应的国家,城市
首先说明,这里是提供的一种思路,纯粹的学习研究所用,已经测试过了,根据IP来定位,基本的误差在1-2公里,主要是因为IP地址是随机分配的,不能精确,写有个小例子,大家也可以封装成自己想要的东西,所提供的是一...
GeoIp2:通过Ip获取地理信息,GeoLite2-City_20220308.zip
maxmind geoip城市库。nginx geoip模块依赖的city库。.mmd格式。根据ip获取城市,国家,大洲等信息。
IP解析地址,python使用起来更方便,geoip2根据Ip获取经纬度和城市的时候需要用到的工具.使用方式 geoip2.database.Reader('GeoLite2-City.mmdb'),geoip2根据Ip获取经纬度和城市的时候需要用到的工具.使用方式 ...
通过这几个库,你可以通过ip地址反查ip所在的国家,地区和街道等信息,适用于根据IP地址获取客户端所在地理位置分析统计的一些功能。 之前我上传过另外一个库https://download.csdn.net/download/pbymw8iwm/13129258...
这是一个ip地址与地理位置信息的一个数据库。通过程序编写连接数据库,可用于根据传入的ip地址,去解析所属的国家,城市。
官网下载,最新GeoLite2/ GeoIP 全球国家IP定位离线库 2020.09.08 最新版 (mmdb)
geoip - 搜索GeoIP数据库对于一个给定的主机或IP地址,并返回IP地址分配的国家,城市,ISP和其他信息
官网最新下载,GeoLite2/ GeoIP 全球国家IP定位离线库 2020.08.04 最新版 (mmdb)
GeoIP最新版数据库, 获取用户所在地国家,城市,经纬度,时区等信息,附带示例代码, 简单方便使用