Home  >  Article  >  Backend Development  >  How to implement access ban in php

How to implement access ban in php

藏色散人
藏色散人Original
2022-11-13 09:12:272163browse

php method to implement access prohibition: 1. Create a php sample file; 2. Pass "if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {. ..}" method to implement IP access restrictions.

How to implement access ban in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to implement access ban in php?

Restrict ip segment access and prohibit ip submission form in php

In project applications, we often need to restrict ip segment access or restrict IP submission Forms and other IP-related functions, today I will share the code I use, I hope it will be helpful to everyone

Just add the following code to the page where you need to prohibit access or submit the form to make a judgment. .

Note: The following is just an example code of PHP restricting IP. If you plan to apply it to CMS, please modify it yourself.

<?php 
/加IP访问限制 
if(getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), &#39;unknown&#39;)) { 
$userip = getenv(&#39;HTTP_CLIENT_IP&#39;); 
} elseif(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), &#39;unknown&#39;)) { 
$userip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;); 
} elseif(getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), &#39;unknown&#39;)) { 
$userip = getenv(&#39;REMOTE_ADDR&#39;); 
} elseif(isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) { 
$userip = $_SERVER[&#39;REMOTE_ADDR&#39;]; 
} 
//限制ip 
if ($userip==&#39;192.168.1.88&#39;){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
} 
//限制ip段 
$ip_arr = explode(&#39;.&#39;, $userip); 
#限制的ip段,假设是192.168.*.* 
if (!(($ip_arr[0] == &#39;192&#39; && $ip_arr[1]==&#39;168&#39;) )){ 
header("location:http://t.qq.com/wb631992791");//被禁止后跳转到微博
exit; 
}else{ 
header("location:http://afish.cnblogs.com");//正常IP则直接访问小鱼阁首页 
exit; 
} 
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement access ban 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