How to prohibit domestic ip from visiting websites by php
This article mainly explains "how php forbids domestic ip to visit the website". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how php prohibits domestic ip from visiting the website".
Php forbids domestic ip to visit the website: 1, to obtain the ip address through "$_ SERVER ['REMOTE_ADDR']"; 2, to judge and prohibit domestic ip from visiting the website through "if ((! empty ($banned [' data'] ['country_id']) {...}".
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
How does php prohibit domestic ip from visiting the website?
Restrict domestic IP access to our website with php code
Principle:
Use the IP interface of Taobao to determine whether IP is a domestic ip, and access is not allowed if it is domestic (CN).
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 Thank you for your reading. The above is the content of "how php prohibits domestic ip from visiting websites". After the study of this article, I believe you have a deeper understanding of how php prohibits domestic ip from visiting websites, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!