Magento 屏蔽国内IP,允许国内特定地区的IP访问

我们做外贸的朋友,可能有这样的一个需求,为了防止同行采集我们网站的数据,我们往往要对竞争对手做一些屏蔽操作
针对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;
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值