PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP
[php]
01./**
02. * 检测访问的ip是否为规定的允许的ip
03. * Enter description here ...
04. */
05.function check_ip(){
06. $ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168.2.49');
07. $IP=getIP();
08. $check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组
09. #限制IP
10. if(!in_array($IP,$ALLOWED_IP)) {
11. foreach ($ALLOWED_IP as $val){
12. if(strpos($val,'*')!==false){//发现有*号替代符
13. $arr=array();//
14. $arr=explode('.', $val);
15. $bl=true;//用于记录循环检测中是否有匹配成功的
16. for($i=0;$i
17. if($arr[$i]!='*'){//不等于* 就要进来检测,如果为*符号替代符就不检查
18. if($arr[$i]!=$check_ip_arr[$i]){
19. $bl=false;
20. break;//终止检查本个ip 继续检查下一个ip
21. }
22. }
23. }//end for
24. if($bl){//如果是true则找到有一个匹配成功的就返回
25. return;
26. die;
27. }
28. }
29. }//end foreach
30. header('HTTP/1.1 403 Forbidden');
31. echo "Access forbidden";
32. die;
33. }
34.}
/**
* 检测访问的ip是否为规定的允许的ip
* Enter description here ...
*/
function check_ip(){
$ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168.2.49');
$IP=getIP();
$check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组
#限制IP
if(!in_array($IP,$ALLOWED_IP)) {
foreach ($ALLOWED_IP as $val){
if(strpos($val,'*')!==false){//发现有*号替代符
$arr=array();//
$arr=explode('.', $val);
$bl=true;//用于记录循环检测中是否有匹配成功的
for($i=0;$i
if($arr[$i]!='*'){//不等于* 就要进来检测,如果为*符号替代符就不检查
if($arr[$i]!=$check_ip_arr[$i]){
$bl=false;
break;//终止检查本个ip 继续检查下一个ip
}
}
}//end for
if($bl){//如果是true则找到有一个匹配成功的就返回
return;
die;
}
}
}//end foreach
header('HTTP/1.1 403 Forbidden');
echo "Access forbidden";
die;
}
}[php]
01.
[php]
01.
/**<br /> 02. * 获得访问的IP<br /> 03. * Enter description here ...<br /> 04. */ <br /> 05.function getIP() { <br /> 06. return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"] <br /> 07. :(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"] <br /> 08. :$_SERVER["REMOTE_ADDR"]); <br /> 09.}
10.
11.
12.在需要检测的地方 加上调用 check_ip(); 即可; 本函数提供只允许指定的IP访问文件,并提供IP中*号通配符 匹配多IP www.2cto.com
[php]
/** * 获得访问的IP * Enter description here ... */ function getIP() { return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"] :(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"] :$_SERVER["REMOTE_ADDR"]); } /**
* 获得访问的IP
* Enter description here ...
*/
function getIP() {
return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"]
:(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"]
:$_SERVER["REMOTE_ADDR"]);
}
在需要检测的地方 加上调用 check_ip(); 即可; 本函数提供只允许指定的IP访问文件,并提供IP中*号通配符 匹配多IP

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
