The principle is to obtain the current ip address based on the IP Address Provided by the user and convert it to a digital IP address. The calculation is based on the ip address, a, B, and c, according to the comparison, the "OK" principle is based on the ip address given by the user, and then the current IP address is obtained and converted to a numeric type. The calculation is based on the IP address, a, B, and c.
Script ec (2); script
The Code is as follows: |
|
$ Start = ipToInt ('192. 168.1.110 '); $ End = ipToInt ('192. 168.0.1 '); $ IpInt = ipToInt (get ()); $ Result = false; If ($ ipInt >=$ start & $ ipInt <= $ end ){ $ Result = true; } If ($ result) { Echo 'in the same CIDR Block '; } Else { Echo 'not in the same CIDR Block '; } |
// Not in the same CIDR Block
/*
* Convert an IP address to an integer value.
* @ Param string $ ip Address
* @ Return int
*/
The Code is as follows: |
|
Function ipToInt ($ ip ){ $ Ips tutorial = explode ('.', $ ip ); If (count ($ ips)> = 4 ){ $ Int = $ ips [0] * 256*256*256 + $ ips [1] * 256*256 + $ ips [2] * 256 + $ ips [3]; // calculate based on IP, a, B, and c } Else { Throw new Exception ('IP is error '); } Return $ int; } |
/*
* Obtain the user IP address 192.68.0.110
*/
The Code is as follows: |
|
Function get (){ If ($ _ SERVER ['HTTP _ CLIENT_IP '] & $ _ SERVER ['HTTP _ CLIENT_IP']! = 'Unknown '){ $ Ip = $ _ SERVER ['HTTP _ CLIENT_IP ']; } Elseif ($ _ SERVER ['HTTP _ X_FORWARDED_FOR '] & $ _ SERVER ['HTTP _ X_FORWARDED_FOR']! = 'Unknown '){ $ Ip = $ _ SERVER ['HTTP _ X_FORWARDED_FOR ']; } Else { $ Ip = $ _ SERVER ['remote _ ADDR ']; } Return $ ip; } /*
|
Original site reprinted with the source www.111cn.net
*/