- 安装方法
http://php.net/manual/en/geoip.installation.php
geoip中的PHP函数介绍:
http://php.net/manual/en/book.geoip.php
若Centos安装提示没有程序包,请执行:
rpm -ivh http://ftp.jaist.ac.jp/pub/Linux/Fedora/epel/6/i386/epel-release-6-8.noarch.rpm
Cpanel中安装不成功请参考链接:
http://infantiablue.tumblr.com/post/264539259/install-geoip-on-cpanel-centos-server - 案例
<?php $countryArray =array( 'USA' //United States ,'GBR' //United Kingdom ,'AUT' //Australia ,'DEU' //Germany ,'ITA' //Italy ,'CAN' //Canada ,'FRA' //France ,'NZL' //New Zealand ,'JPN' //Japan ,'ESP' //Spain ,'CHN' //China ); $fileName = $_GET['fileName']; if(isset($fileName)){ $country_code = geoip_country_code3_by_name($_SERVER["REMOTE_ADDR"]); if(in_array($country_code, $countryArray)) header("Location: http://www.google.com/".$fileName); else header("Location: http://www.baidu.com/".$fileName); } ?>
保存上面的代码到PHP文件中,命令为file.php.
访问:http://www.youwebsite.com/file.php?fileName=abc
若访问的IP国家在CountryArray列表中,则链接跳转到:http://www.google.com/abc
若不属于CountryArray列表,则链接到:http://www.baidu.com/abc - 验证geoip是否生效函数
echo $_SERVER["REMOTE_ADDR"].'<br>';
$country_code = geoip_country_code3_by_name($_SERVER["REMOTE_ADDR"]);
$country_code = geoip_country_code3_by_name('58.186.0.0');
echo $country_code.'<br>';