Yesterday to determine the user IP source, and then enter the user IP address of the network segment, we first need to IP address and integer conversion function implementation, let me see
System Functions Ip2long and Long2ip
PHP has built-in functions ip2long can convert IP addresses to integer types.
The code is as follows |
Copy Code |
$ip = ' 210.110.11.49 '; echo Ip2long ($IP); Output: -764540111 |
The integral type of the output has a minus sign because we get the result is signed integer, signed integer maximum 2147483647, to convert the result to unsigned can be so written
3530427185
Convert an integer to an IP address using LONG2IP
The code is as follows |
Copy Code |
$ip = ' 210.110.11.49 '; $ip _int = Ip2long ($IP); echo $ip. " "; echo $ip _int. " "; Echo Long2ip ($ip _int); Output:
210.110.11.49 -764540111 210.110.11.49 |
As you can see from the results, the IP and integer types can be done through functions.
system function Small Bug
This bug online A search is, to the effect that the IP segment plus a leading 0, first to see the bug instance
The code is as follows |
Copy Code |
$ip = ' 210.110.011.49 ';
$ip _int = Ip2long ($IP); echo $ip. " "; echo $ip _int. " "; Echo Long2ip ($ip _int); Output:
210.110.011.49 -764540623 210.110.9.49 |
The conversion results do not match, we try to add a leading 0 before the first digit of the IP, and then look at
The code is as follows |
Copy Code |
$ip = ' 021.110.11.49 ';
$ip _int = Ip2long ($IP); echo $ip. " "; echo $ip _int. " "; Echo Long2ip ($ip _int); Output:
021.110.11.49 292424497 17.110.11.49 |
The result of the conversion was wrong. All of the above examples are caused by a leading 0, resulting in an error in the conversion result, and the reversal result does not match the original conversion IP
Conversion principle
There are currently two algorithms:
First, the first paragraph multiplied by 256 of the three square, the second segment multiplied by 256 squared, the third paragraph multiplied by 256, the last sum
The code is as follows |
Copy Code |
$ip = ' 0210.110.11.49 '; function Iptoint ($IP) {
$iparr = Explode ('. ', $IP); $num = 0; for ($i =0; $i<>< p=""><> $num + = Intval ($iparr [$i]) * POW (256,count ($iparr)-($i + 1)); } return $num; } echo $ip. ' ';
$ip _int = Iptoint ($IP); echo $ip _int. ' '; Echo Long2ip ($ip _int); Output: 0210.110.11.49 3530427185 210.110.11.49 |
Second, through bitwise operators
The code is as follows |
Copy Code |
$ip = ' 0210.110.11.49 ';
function Iptoint ($IP) {
$iparr = Explode ('. ', $IP); Return (Intval ($iparr [0]<<24)) | (Intval ($iparr [1]) <<16) | (Intval ($iparr [2]) <<8) | (Intval ($iparr [3])); } echo $ip. ' ';
$ip _int = Iptoint ($IP); echo $ip _int. ' '; Echo Long2ip ($ip _int); Output:
0210.110.11.49 -764540111 210.110.11.49 |
Detect if IP is legitimate
First, self-traversal detection
The code is as follows |
Copy Code |
function Check_ip ($IP) {
$iparr = Explode ('. ', $IP); foreach ($iparr as $v) {if ($v >255) return false;} return true; } Echo ' 210.285.11.49, '; Var_dump (check_ip (' 210.285.11.49 ')); Echo ' '; Echo ' 210.205.11.49, '; Var_dump (check_ip (' 210.205.11.49 ')); Output:
210.285.11.49,bool (False) 210.205.11.49,bool (True) |
Second, use Ip2long to return
The code is as follows |
Copy Code |
function Check_ip ($IP) {
if (Ip2long ($IP)) return true; return false; } Echo ' 210.285.11.49, ';
Var_dump (check_ip (' 210.285.11.49 ')); Echo ' '; Echo ' 210.205.11.49, '; Var_dump (check_ip (' 210.205.11.49 ')); Output:
210.285.11.49,bool (False) 210.205.11.49,bool (True) |
http://www.bkjia.com/PHPjc/631547.html www.bkjia.com true http://www.bkjia.com/PHPjc/631547.html techarticle yesterday to determine the user IP source, and then enter the user IP address of the network segment, we first need to the IP address and integer conversion function implementation, let me look at the system functions ...