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.
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; }
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!