Home Backend Development PHP Tutorial PHP 中检查或过滤IP地址的实现代码_php技巧

PHP 中检查或过滤IP地址的实现代码_php技巧

May 17, 2016 am 09:14 AM
IP address filter

你可以通过增加一个配置文件,然后将需要禁止的一些 IP 地址通过一定规则添加到配置文件中,在程序初始化的时候,读取配置文件中的每个规则,然后通过本文提供的方法去检查当前访问的客户端 IP 地址是否存在于这些规则中,如果存在,则拒绝提供服务。

复制代码 代码如下:

/**
* PHP 中检查或过滤 IP 地址
*
* 支持 IP 区间、CIDR(Classless Inter-Domain Routing)及单个 IP 格式
* 整理:http://www.CodeBit.cn
* 参考:
* - {@link http://us2.php.net/manual/zh/function.ip2long.php#70055}
* - {@link http://us2.php.net/manual/zh/function.ip2long.php#82397}
*
* @param string $network 网段,支持 IP 区间、CIDR及单个 IP 格式
* @param string $ip 要检查的 IP 地址
* @return boolean
*/
function netMatch($network, $ip) {
$network = trim($network);
$ip = trim($ip);
$result = false;
// IP range : 174.129.0.0 - 174.129.255.255
if (false !== ($pos = strpos($network, "-"))) {
$from = ip2long(trim(substr($network, 0, $pos)));
$to = ip2long(trim(substr($network, $pos+1)));
$ip = ip2long($ip);
$result = ($ip >= $from and $ip // CIDR : 174.129.0.0/16
} else if (false !== strpos($network,"/")) {
list ($net, $mask) = explode ('/', $network);
$result = (ip2long($ip) & ~((1 // single IP
} else {
$result = $network === $ip;
}
return $result;
}
// 174.129.0.0 - 174.129.255.255
var_dump(netMatch(' 174.129.0.0 - 174.129.255.255 ', '174.129.1.31')); // True
var_dump(netMatch(' 174.129.0.0/16 ', '174.139.1.31')); // False
var_dump(netMatch(' 174.129.1.32 ', '174.129.1.31')); // False
?>

由于中国使用的大多数都是动态 IP 地址,所以通过 IP 地址限制访问具有一定的局限性,使用的时候需要谨慎,但是对于应急限制访问来说,还是非常有用的。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set Xiaohongshu not to display IP address? How does it change the id to locate the city? How to set Xiaohongshu not to display IP address? How does it change the id to locate the city? Mar 27, 2024 pm 03:00 PM

Xiaohongshu is a popular social e-commerce platform where users can share their daily life and discover their favorite products. Some users are more sensitive to personal privacy and hope that their IP address will not be displayed on Xiaohongshu to protect their online privacy. So, how to set Xiaohongshu not to display the IP address? This article will answer this question in detail. 1. How to set Xiaohongshu not to display the IP address? 1. Modify Xiaohongshu settings: Open Xiaohongshu APP, click "Me" in the lower right corner to enter the personal center. Then click on the avatar to enter account settings. In the account settings, find "Privacy Settings" and click to enter. Here, you can find the setting options for IP address, just turn it off. 2. Clear cache: Sometimes, Xiaohongshu may display an error

How to solve the problem that Windows 10 IP address cannot be saved after setting it How to solve the problem that Windows 10 IP address cannot be saved after setting it Jul 08, 2023 pm 12:33 PM

Sometimes everyone encounters the need to manually modify the IP address, but some Windows 10 customers report that the IP address cannot be saved after setting it. How to solve this situation? The IP address is stored basically because there is an error in filling in your IP address. You can check whether the subnet prefix has been written with the subnet mask. If so, change it back. After the change, you can save it normally. IP address. How to solve the problem that the Windows 10 IP address cannot be saved after it is set: The error picture is as follows: The prompt "Unable to save the IP setting, please check one or more settings and try" caused by filling in the error. This is the subnet prefix length, not the subnet mask. as the picture shows. As shown in the picture above, many users actually write out the subnet prefix as the subnet mask.

Where is the IP address of Xiaomi mobile phone? Where is the IP address of Xiaomi mobile phone? Feb 29, 2024 pm 06:10 PM

Where is the IP address of Xiaomi mobile phone? You can check the IP address on Xiaomi mobile phone, but most users don’t know where to check the IP address. Next is the graphic tutorial on how to check the IP address of Xiaomi mobile phone brought by the editor. Interested users come and take a look! Where is the IP address of Xiaomi mobile phone? 1. First open the settings function in Xiaomi mobile phone, select [My Device] and click to enter; 2. Then on the My Device function page, click [All Parameters] service; 3. Then on the All Parameters page , slide to the bottom and select [Status Information]; 4. Finally, you can see the IP address in the status information interface.

Where to change the IP address of Xianyu_Share how to change the IP address of Xianyu Where to change the IP address of Xianyu_Share how to change the IP address of Xianyu Mar 20, 2024 pm 05:06 PM

Xianyu is a very practical second-hand trading platform. Here we can buy many different products and sell our own idle items. What if we want to modify our address? Let’s take a look with the editor below! Share how to modify the Xianyu IP address. First, open the Xianyu software. After entering the homepage, you can see seafood market, recommendations, address and other options in the upper left corner. Click "Address". 2. Then on the address page, we click the [Down Arrow] next to the address; 3. After the final click, we click on the city on the city selection page;

How to change the location of Douyin IP address? Why does the IP address change location? How to change the location of Douyin IP address? Why does the IP address change location? Mar 21, 2024 pm 06:30 PM

Users share their lives, show off their talents, and interact with netizens across the country and even the world through Douyin. Some users wish to change their IP addresses on Douyin due to reasons such as privacy protection or geographical restrictions. So, how does the Douyin IP address change its location? 1. How to change the location of Douyin IP address? A proxy server is an intermediary service used to forward user requests to the Internet and return responses. By configuring a proxy server, users can hide their real IP addresses and change their IP addresses. This approach helps protect user privacy and improves network security. Proxy servers can also be used to access restricted content or bypass geolocation restrictions. Overall, using a proxy server is a practical network tool that can help users browse the Internet more safely and freely.

Bitcoin transaction IP address (Is the Bitcoin transaction IP address public?) Bitcoin transaction IP address (Is the Bitcoin transaction IP address public?) Feb 06, 2024 am 10:03 AM

Bitcoin transaction IP address Bitcoin transaction IP address is an indispensable and important component of the Bitcoin transaction system. It is the core of the Bitcoin trading platform through which Bitcoin traders can conduct Bitcoin transactions. The Bitcoin transaction IP address is the basis of the Bitcoin transaction system and the basis on which Bitcoin traders can conduct Bitcoin transactions. The Bitcoin trading IP address is a global network address used to locate the Bitcoin trading system’s servers and traders’ devices. By querying the Bitcoin transaction IP address, you can obtain transaction status and related information. In addition, Bitcoin trading IP addresses can also be used to connect clients to the Bitcoin trading system and traders’ devices. Are Bitcoin transaction IP addresses public? Bitcoin transaction IP addresses will not be made public

Go Regular Expression Guide: How to Match IP Addresses Go Regular Expression Guide: How to Match IP Addresses Jul 12, 2023 am 11:09 AM

Go Regular Expressions Guide: How to Match IP Addresses Introduction: Regular expressions are a powerful tool for matching and searching for specific patterns in strings. In the Go language, you can use the built-in regexp package to implement regular expression operations. This article will introduce how to use regular expressions in Go language to match IP addresses. IP address format: An IP address is an address composed of 32-bit binary numbers, usually represented by four separated decimal numbers. Each decimal number ranges from 0 to 255, for example:

Where can I change the printer's IP address? Tutorial on changing printer IP address Where can I change the printer's IP address? Tutorial on changing printer IP address Mar 13, 2024 pm 10:16 PM

The IP address is the unique identification of the host on the network. Of course, the printer also has an IP address, so can the printer's IP address be changed? How to change it? First, we open the computer's settings options, find the printer and scanner options, then open the printer properties page, and finally click on the port configuration option to change the printer's IP address. How to change the printer's IP address: 1. Click Start in the lower left corner, then enter settings, and then click "Devices". 2. Click "Printers and Scanners" on the left taskbar. 3. Click "Manage" under the printer, click "Printer Properties&" on the left

See all articles