上次介绍过一个ip2nation.com,需要安装数据库,这里介绍另一个更简单,但是同样有效的方法。
1.下载2个文件
http://geolite.maxmind.com/downl ... ountry/GeoIP.dat.gz(解压为GeoIP.dat,重命名为geoip.dat)
http://geolite.maxmind.com/download/geoip/api/php/geoip.inc(用迅雷或文件另存为下载)
2.在index.php的同目录新建一个geoip文件夹,把文件geoip.dat和geoip.inc上传至此。
3.打开index.php,在最前面插入代码- <?php
- // PUT THIS CODE AT THE TOP OF YOUR INDEX.PHP BEFORE ANYTHING ELSE
- include('geoip/geoip.inc'); //this file must exist in your directory
- // Uncomment if querying against GeoIP/Lite City.
- // include('geoip/geoip.inc');
- $gi = geoip_open('geoip/geoip.dat',GEOIP_STANDARD);
- // get the ip of the visitor
- $addr = getenv('REMOTE_ADDR');
- // translate his ip to a country code
- $country = geoip_country_name_by_addr($gi, $addr);
- // close the geo database
- geoip_close($gi);
- $badcountry = array("Iran, Islamic Republic of","India","Brazil");
- // redirect them if they suck
- if(in_array($country, $badcountry))
- header('Location: [url]http://www.google.com/'[/url]); //enter a url or page on your site
- ?>
复制代码 $badcountry = array("Iran, Islamic Republic of","India","Brazil");
红颜色的是不需要的国家,我这里转向的是伊朗、印度、巴西,具体的国家名你可以在geoip.inc,这个文件里查到
header('Location: http://www.google.com/');
绿颜色的是转向到的网站,我这里是转到google,当然实际情况你可以转向到自己的其他网站。 |