我们做外贸的朋友,可能有这样的一个需求,为了防止同行采集我们网站的数据,我们往往要对竞争对手做一些屏蔽操作
针对Magento系统做了一个屏蔽模块
这里我们推荐使用https://www.ipinfodb.com/ ,YOUR_API_KEY自己去申请,虽然说是完全免费的,但是还是有查询限制:我们没有特定的每日限制,但是查询速度超过每秒2次将被阻塞。如果你的查询次数低于2次/秒,一切正常
/****START 屏蔽国内IP模块*******/
// Load the class
include './class.IPInfoDB.php';
$ipinfodb = new IPInfoDB('YOUR_API_KEY');
//get client ip
$results = $ipinfodb->getCity(getClientIP());
if(($results['countryCode']=="CN")){
if(($results['regionName']=="Hubei")){
}else{
header("Location: http://www.xxx.ru/nddbc.html");
exit();
}
}
function getClientIP()
{
global $ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else $ip = "Unknow";
return $ip;
}
/****END 屏蔽国内IP模块********/
class.IPInfoDB.php
<?php
class IPInfoDB
{
protected $apiKey;
public function __construct($apiKey)
{
if (!preg_match('/^[0-9a-z]{64}$/', $apiKey)) {
throw exception('Invalid IPInfoDB API key.');
}
$this->apiKey = $apiKey;
}
public function getCountry($ip)
{
$response = @file_get_contents('http://api.ipinfodb.com/v3/ip-country?key=' . $this->apiKey . '&ip=' . $ip . '&format=json');
if (($json = json_decode($response, true)) === null) {
$json['statusCode'] = 'ERROR';
return false;
}
$json['statusCode'] = 'OK';
return $json;
}
public function getCity($ip)
{
$response = @file_get_contents('http://api.ipinfodb.com/v3/ip-city?key=' . $this->apiKey . '&ip=' . $ip . '&format=json');
if (($json = json_decode($response, true)) === null) {
$json['statusCode'] = 'ERROR';
return false;
}
$json['statusCode'] = 'OK';
return $json;
}
}