Restrict access to IP addresses in php

Source: Internet
Author: User
Tags block ip address
Write your permitted ip address range first. If you do not know, you need to go to the Internet to find an ip table to see a single IP address: if ($ REMOTE_ADDR12.34.56.78) {allow access} if it is a group of IP addresses, it depends on the range of these IP addresses and whether consecutive IP addresses are available, and non-consecutive IP addresses have to be compared, affecting the speed. Restrict a source ip address.

Write your permitted ip address range first. If you do not know, go to the Internet to find an ip table.

Single IP address:

If ($ REMOTE_ADDR = '12. 34.56.78 ')
{
// Allow access
}


If it is a group of IP addresses, it depends on the IP address range and whether it is continuous.
Continuous IP addresses are easier to handle. non-continuous IP addresses must be compared to each other, affecting the speed.

Ip address restriction source code:

<%
If ($ GLOBALS ["CanMaskIp"] = 1) // block an IP address
{
$ StrSQL = "SELECT count (*) FROM MaskIp WHERE '". $ GLOBALS ["strVisitedIp"]. "'Between SrcIpAddr AND DesIpAddr ";
$ TmpMaskIp = mysql_fetch_row (mysql_query ($ strSQL, $ GLOBALS ["hDatabase"]);
If ($ tmpMaskIp [0]> 0)
{
Echo"\ N ";
Echo"\ N ";
Echo"". $ GLOBALS [" WindowTitle "]."\ N ";
Echo"\ N ";
Echo"\ N ";
Echo"

  

\ N ";
Echo"

  

\ N ";
Echo"

  

\ N ";
Echo"

  

\ N ";
Echo"

Hey, your IP address is forbidden by the administrator !!
\ N ";
Echo" \ N ";
Echo "think about what you did, huh ?! \ N ";
Echo"

\ N ";
Echo"\ N ";
Echo"";
Mysql_close ($ GLOBALS ["hDatabase"]);
Exit ();
}
Unset ($ tmpMaskIp );
}
%>

The following table must be created:

Create table MaskIp (
SrcIpAddr varchar (16) not null default '',
DesIpAddr varchar (16) not null default '',
Primary key (SrcIpAddr)
) TYPE = MyISAM COMMENT = 'Record and block IP address ';



Function chkip ($ client_ip, $ network_number, $ ip_mask)
{
Preg_match ("/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/", $ client_ip, $ ipsplit );
Preg_match ("/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/", $ ip_mask, $ ipsplit1 );
Preg_match ("/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/", $ network_number, $ ipsplit2 );
If (integer) $ ipsplit [1] & $ ipsplit1 [1]) = $ ipsplit2 [1])
If (integer) $ ipsplit [2] & $ ipsplit1 [2]) = $ ipsplit2 [2])
If (integer) $ ipsplit [3] & $ ipsplit1 [3]) = $ ipsplit2 [3])
If (integer) $ ipsplit [4] & $ ipsplit1 [4]) = $ ipsplit2 [4])
Return true;
Return false;
}
?>

One function parameter is the client ip address, followed by the network segment comparison method,
Returns true if the IP address and mask are the same as the IP address and mask.
For the example used by the landlord, the call is as follows:
Chkip ($ REMOTE_ADDR, "202.112.96.0", "255.255.255.0 ")
Read the prohibited list to the memory and compare them one by one ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.