This article is original, reprint please specify!
In our development of multi-site business site, often need to obtain the client's IP address to give users to recommend their location information business, with PHP to obtain the client's IP address, we generally use the PHP built-in method is $_server[' REMOTE_ADDR '.
However, this function can only get the IP address set in the visitor's local connection, the LAN gateway exit IP addresses, if the visitor uses a proxy server, will not get the proxy server IP, but to obtain the real IP of the visitor gateway. If you apply this function to a Web page that is restricted to IP access, other people will not be able to access the page even if they access the proxy server in the segment via IP. So we generally take the following approach to prevent spoofing:
/** * GET client IP address * @param integer $type return type 0 return IP address 1 return IPV4 address number * @param boolean $ADV whether Advanced mode acquisition (possibly disguised) * @return mi Xed*/functionGET_CLIENT_IP ($type= 0,$adv=false) { $type=$type? 1:0; Static $ip=NULL; if($ip!==NULL)return $ip[$type]; if($adv){//Advanced Mode Acquisition (prevents spoofing) if(isset($_server[' Http_x_forwarded_for '])) { $arr=Explode(‘,‘,$_server[' Http_x_forwarded_for ']); $pos=Array_search(' Unknown ',$arr); if(false!==$pos)unset($arr[$pos]); $ip=Trim($arr[0]); }ElseIf(isset($_server[' Http_client_ip '])) { $ip=$_server[' Http_client_ip ']; }ElseIf(isset($_server[' REMOTE_ADDR '])) { $ip=$_server[' REMOTE_ADDR ']; } }ElseIf(isset($_server[' REMOTE_ADDR '])) { $ip=$_server[' REMOTE_ADDR ']; } //IP Address legal authentication $long=sprintf("%u",Ip2long($ip)); $ip=$long?Array($ip,$long) :Array(' 0.0.0.0 ', 0); return $ip[$type];
The code uses two PHP built-in Functions sprintf () and Ip2long (), these two functions are explained below:
How do I convert four fields with a point-separated IP address protocol to an integer? PHP has such a function ip2long.
<?php
Echo Ip2long ("10.2.1.3");
?>
We will get
167903491
How is this calculated?
<?PHPfunctionIp2int ($ip){ //we'll divide the IP into four segments, $ip 1, $ip 2, $ip 3, $ip 4 List($ip 1,$ip 2,$ip 3,$ip 4)=Explode(".",$ip); //then the first paragraph is multiplied by 256 of the three squares, the second is multiplied by 256 squared, the third is multiplied by 256//This is the value we get return $ip 1*POW(256,3) +$ip 2*POW(256,2) +$ip 3*256+$ip 4;}?>
When the IP address is large, Ip2long will appear negative:
<? PHP $ip = ' 192.168.101.100 '; $ip _long Ip2long ($ip); Echo $ip _long. Php_eol; // -1062705820 Echo Long2ip ($ip _long// 192.168.101.100?>
What is this for?
The IPV4 uses unsigned 32-bit addresses, so there are up to 2 32-time minus 1 (4294967295) addresses. Write a 10 binary number separated by 4 decimal points.
Recorded as A.B.C.D, for example: 192.168.100.100.
IPV4 address each 10 binary number is an unsigned byte, in the range of 0~255, the IPV4 address to an unsigned number, in fact, each 10 binary number is placed on the corresponding 8 bits, a 4-byte unsigned integer form. 192.168.100.100,192,168 in high 8 bits 100,100 in low 8 bits.
Workaround:
The output is formatted as an unsigned integer with%u. When you keep the IP data in the database (MySQL), we are accustomed to using the Ip2long function to generate an integer type, and then store it in an int (11) Type field, but on different system platforms, the value of the Ip2long function is
Different, so it may cause errors in reading data from the database, using LONG2IP to get the IP, and say the situation we have encountered:
We use an int (11) type (range-2147483648-2147483647) to save the result of processing an IP address with Ip2long, for example, the IP is ' 202.105.77.179′ ', then the result on the 32-bit machine is:- 899068493,
On a 64-bit machine, it gets 3395898803. It is then written to the database, because it exceeds the range of int (11), so the result on the 64-bit machine is saved as the maximum value of int (11): 2147483647. So when you take it out of the database, you get the wrong result, 'll have
To "127.255.255.255″ this IP address.
There are a number of workarounds, such as MySQL functions: Inet_aton and Inet_ntoa to handle IP addresses, or changing the field of the saved IP address to bigint type, so that the 64-bit machine has 3395898803 saved. Use the LONG2IP function to still get the correct results
We will find that some IP is converted to an integer, which is negative, because the resulting result is a signed integer, the maximum value is 2147483647. To convert it to unsigned, you can use
sprintf ("%u", Ip2long ($IP);
Can be converted to a positive integer. And the result can be converted back to the original IP address with Long2ip. You can also use Ip2long to verify that an IP is valid
Get user Login IP address and general processing in PHP