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

How to ban domestic IP from accessing the website in PHP

藏色散人
Release: 2023-03-13 14:06:01
Original
4220 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template