转向不需要国家的IP

[复制链接]
查看: 1054   回复: 1
发表于 2009-2-2 22:10:00 | 显示全部楼层 |阅读模式
上次介绍过一个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,在最前面插入代码
  1. <?php
  2. // PUT THIS CODE AT THE TOP OF YOUR INDEX.PHP BEFORE ANYTHING ELSE
  3. include('geoip/geoip.inc'); //this file must exist in your directory
  4. // Uncomment if querying against GeoIP/Lite City.
  5. // include('geoip/geoip.inc');
  6. $gi = geoip_open('geoip/geoip.dat',GEOIP_STANDARD);
  7. // get the ip of the visitor
  8. $addr = getenv('REMOTE_ADDR');
  9. // translate his ip to a country code
  10. $country = geoip_country_name_by_addr($gi, $addr);
  11. // close the geo database
  12. geoip_close($gi);

  13. $badcountry = array("Iran, Islamic Republic of","India","Brazil");

  14. // redirect them if they suck
  15. if(in_array($country, $badcountry))
  16. header('Location: [url]http://www.google.com/'[/url]); //enter a url or  page on your site
  17. ?>
复制代码
$badcountry = array("Iran, Islamic Republic of","India","Brazil");
红颜色的是不需要的国家,我这里转向的是伊朗、印度、巴西,具体的国家名你可以在geoip.inc,这个文件里查到

header('Location: http://www.google.com/');
绿颜色的是转向到的网站,我这里是转到google,当然实际情况你可以转向到自己的其他网站。

评分

参与人数 2 +32 收起 理由
nod32 + 2
回忆月光 + 30

查看全部评分

发表于 2009-2-2 22:15:31 | 显示全部楼层
我正需要,thx
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则