【转】GeoIP + PHP 完全指南
2011-03-08 19:15
302 查看
转自:http://www.21andy.com/blog/20100405/1799.html
有2种标准方法来使用 GeoIP
第一种方法:
先下载 GeoIP 数据文件
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip GeoIP.dat.gz 接着下载 GeoIP 的 PHP 文件,保存为 geoip.php
http://geolite.maxmind.com/download/geoip/api/php/geoip.inc
好了,直接使用
<?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.php");
// 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.";
?> 此方法使用二进制文件做为数据库,速度很快,不需要数据库。我试了执行时间是0.001,每秒完成1000次不成问题。
第二种方法:
把 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/bin/phpize
./configure --with-php-config=/usr/bin/php-config --with-geoip
make
make install 在 php.ini 里加上
extension=geoip.so 接着重启一下 php 就行了
service httpd restart 现在,你可以使用 php 手册里的 GeoIP 部份函数了
http://cn.php.net/manual/en/book.geoip.php
有2种标准方法来使用 GeoIP
第一种方法:
先下载 GeoIP 数据文件
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip GeoIP.dat.gz 接着下载 GeoIP 的 PHP 文件,保存为 geoip.php
http://geolite.maxmind.com/download/geoip/api/php/geoip.inc
好了,直接使用
<?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.php");
// 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.";
?> 此方法使用二进制文件做为数据库,速度很快,不需要数据库。我试了执行时间是0.001,每秒完成1000次不成问题。
第二种方法:
把 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/bin/phpize
./configure --with-php-config=/usr/bin/php-config --with-geoip
make
make install 在 php.ini 里加上
extension=geoip.so 接着重启一下 php 就行了
service httpd restart 现在,你可以使用 php 手册里的 GeoIP 部份函数了
http://cn.php.net/manual/en/book.geoip.php
相关文章推荐
- FastCGI 不完全高级指南(PHP版,Windows平台)
- PHP-FPM 不完全指南
- IIS下安装PHP+MySQL完全指南
- Windows环境下完全手工配置Apache、MySQL和PHP
- 给PHP加速,eAccelerator配置和使用指南
- php hash 漏洞手动打补丁指南
- 前端优化不完全指南
- Cygwin完全下载指南
- CI框架源码完全分析之核心文件(输入类)Input.php
- CBROM使用完全指南(中,英说明)
- 基于外部ZooKeeper的GlusterFS作为分布式文件系统的完全分布式HBase集群安装指南
- Android编程规范不完全指南
- PHP加速 eAccelerator配置和使用指南
- systemctl命令完全指南
- 配置整合Win+Apache+PHP+MySQL+Tcomcat(或Resin)完全手册
- 爆详细Ubuntu16.04,CUDA9.1,OpenCV3.1,Tensorflow完全配置指南
- PHP加速 eAccelerator配置和使用指南
- 高可用,完全分布式Hadoop集群HDFS和MapReduce安装配置指南
- boost完全开发指南第2章-处理时间4 (日期区间date_period)
- XGBoost参数调优完全指南(附Python代码)