GeoIP的使用 - PHP版
  4yu2JdefgeRW 2023年11月02日 30 0


我们在做项目的时候,常常需要国家和地区的IP地址,下面通过介绍GeoIp的使用,让你轻松获取世界各地的国家和地区IP地址。

GeoIP介绍:

什么是GepIP ?

所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息。这里面的技术不算难题,关键在于有个精准 的数据库。有了准确的数据源就奇货可居赚点小钱,可是发扬合作精神,集体贡献众人享用是我们追求的。

 

方法1、

1)到官网:http://dev.maxmind.com/geoip/legacy/geolite/ 下载需要的版本

Language/Platform

Documentation

Release History

Package Repository

Source Download

Version Control

Apache (mod_geoip)

Documentation

Releases

 

Source

GitHub

C

Documentation

Releases

DebianUbuntu

Source

GitHub

C#

Documentation

Releases

 

Source

GitHub

Java

Documentation

Releases

Maven

Source

GitHub

Microsoft COM

Documentation (64-bit)

 

 

Source

GitHub

Perl

Documentation

Releases

MetaCPAN

 

GitHub

php

Documentation

Releases

Composer

Source

GitHub

Python

Documentation

Releases

 

Source

GitHub

 

2)http://dev.maxmind.com/geoip/legacy/geolite/ 下载 国家、城市ip库

 

Download links

Database

Binary / gzip

Binary / xz

CSV / gzip

CSV / zip

CSV / xz

GeoLite Country

Download

Gzip only

Zip only

Download

Zip only

GeoLite Country IPv6

Download

Gzip only

Download

Gzip only

Gzip only

GeoLite City

Download

Download

Zip and xz only

Download

Download

GeoLite City IPv6 (Beta)

Download

Gzip only

Download

Gzip only

Gzip only

GeoLite ASN

Download

Gzip only

Zip only

Download

Zip only

GeoLite ASN IPv6

Download

Gzip only

Zip only

Download

Zip only

 

3)找到示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路径为你自己的路径,进行测试

if( !defined( 'DOCUROOT' ) ){ define( 'DOCUROOT', str_replace("geoip","",dirname( __FILE__ ) ));}
include("geoipcity.inc");
include("geoipregionvars.php");

// uncomment for Shared Memory support
// geoip_load_shared_mem("/usr/local/share/GeoIP/GeoIPCity.dat");
// $gi = geoip_open("/usr/local/share/GeoIP/GeoIPCity.dat",GEOIP_SHARED_MEMORY);

$gi = geoip_open(DOCUROOT."geoipGeoLiteCity.dat",GEOIP_STANDARD);

$record = geoip_record_by_addr($gi,"68.32.235.26");
echo "you country code is:" .$record->country_code ;

geoip_close($gi);

 

方法2



把 GeoIP 安装成 php 扩展
yum install GeoIP GeoIP-data GeoIP-devel

下载 GeoIP 数据库
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d GeoLiteCity.dat.gz
mv GeoLiteCity.dat /var/lib/GeoIP/GeoIPCity.dat

下载 GeoIP 的 PECL 扩展
下载地址 http://pecl.php.net/package/geoip
wget -c http://pecl.php.net/get/geoip-1.0.7.tgz
tar -zxvf geoip-1.0.7.tgz

安 装 GeoIP 的 PECL 扩展
cd geoip-1.0.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-geoip
make
make install

在 php.ini 里加上
extension=geoip.so

接着重启一下 php 就行了
现在,你可以使用 php 手册里的 GeoIP 部份函数了

 

实例下载:geoip-api-php-master

数据下载:GeoIPv6.dat  |  GeoIP.dat

下载自我实例:ip_to_location(其实城市还是不太准确啊)

 

如果只是显示国家的话:

include("geoip.inc.php");
// 打开数据文件 
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// 获取国家代码 
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); 
echo "Your country code is: $country_code ";
// 获取国家名称 
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); 
echo "Your country name is: $country_name ";
// 关闭文件 
geoip_close($gi);

 



 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  iFlC2TQwmEmX   2023年12月11日   36   0   0 phpthinkPHP框架
  iFlC2TQwmEmX   2023年12月09日   33   0   0 php
4yu2JdefgeRW