Home  >  Article  >  Backend Development  >  How to ban domestic IP from accessing the website in PHP

How to ban domestic IP from accessing the website in PHP

藏色散人
藏色散人Original
2021-10-27 09:29:174091browse

php method to ban domestic ip from accessing the website: 1. Obtain the ip address through "$_SERVER['REMOTE_ADDR']"; 2. Through "if((!empty($banned['data'][' country_id']){...}" determine and prohibit domestic IP from accessing the website.

How to ban domestic IP from accessing the website in PHP

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 Computer

How to prohibit domestic IPs from accessing the website in php?

Use php code to restrict domestic IPs from accessing our website

##Principle:

Use Taobao’s IP interface to determine whether the IP is a domestic IP. If it is domestic (CN), access is not allowed.

The code is as follows:

$ip = $_SERVER['REMOTE_ADDR'];
$content = file_get_contents(‘http://ip.taobao.com/service/getIpInfo.php?ip=’.$ip);
$banned = json_decode(trim($content), true);
$lan = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if((!empty($banned['data']['country_id']) && $banned['data']['country_id'] == ‘CN’) || strstr($lan, ‘zh’))
{
header(“HTTP/1.0 404 Not Found”);
echo ‘HTTP/1.0 404 Not Found’;
exit;
}

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to ban domestic IP from accessing the website in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn