The security method HTTP_X_FORWARDED_FOR for obtaining user IP addresses on the web site. The code for copying the getIP function after security filtering is as follows: functiongetIP () {$ realip; set the default value if (isset ($ _ SERVER [HTTP_X_FORWARDED_FOR]) {$ realip $ _ SERVER [getIP function after HTTP _ security filter
The code is as follows:
Function getIP (){
$ Realip = ''; // set the default value
If (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Realip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
} Elseif (isset ($ _ SERVER ['http _ CLIENT_IP ']) {
$ Realip = $ _ SERVER ['http _ CLIENT_IP '];
} Else {
$ Realip = $ _ SERVER ['remote _ ADDR '];
}
Preg_match ('/^ ((? : \ D {1, 3} \.) {3} \ d {1, 3})/', $ realip, $ match );
Return $ match? $ Match [0]: false;
}
The above function adds an IP address judgment. it only reads data starting with the Ip address format, and the first one meets the IP format value. If no value is returned, false is returned. In this way, you can read IP addresses in the correct format and verify the IP address format of the data.
If I read the IP address of the Internet and the IP address transmitted by the user to the LAN, I should filter it out directly.
On some websites, we often see prompts that illegal IP addresses are in fact part due to incorrect IP address format, and some may be read to IP addresses, which do not meet the IP address format allowed on the Internet. The following function encapsulates a function through the IANA site specification. By entering an IP address, you can accurately know whether the IP address can be applied on the Internet.
The code is as follows:
// IP addresses allowed on the Internet
Function ipType2 ($ ip ){
$ Iplist = explode (".", $ ip );
If ($ iplist [0]> = 224 & $ iplist [0] <= 239)
Return 'multicast ';
If ($ iplist [0]> = 240 & $ iplist [0] <= 255)
Return 'retain ';
If (preg_match ('/^ 198 \. 51 \. 100/', $ ip ))
Return 'test-NET-2, documents and Example ';
If (preg_match ('/^ 203 \. 0 \. 113/', $ ip ))
Return 'test-NET-3, documents and Example ';
If (preg_match ('/^ 192 \. (18 | 19) \./', $ ip ))
Return 'network benchmarking ';
If (preg_match ('/^ 192 \. 168/', $ ip ))
Return 'private network [intranet] ';
If (preg_match ('/^ 192 \. 88 \. 99/', $ ip ))
Return 'Listen 6to4 relay ';
If (preg_match ('/^ 192 \. 0 \. 2 \./', $ ip ))
Return 'test-NET-1, document and Example ';
If (preg_match ('/^ 192 \. 0 \. 0 \./', $ ip ))
Return 'reserved (IANA )';
If (preg_match ('/^ 192 \. 0 \. 0 \./', $ ip ))
Return 'reserved (IANA )';
If ($ iplist [0] = 172 & $ iplist [1] <= 31 & $ iplist [1]> = 16)
Return 'private network [intranet] ';
If ($ iplist [0] = 169 & $ iplist [1] = 254)
Return 'link local ';
If ($ iplist [0] = 127)
Return 'loopback address ';
If ($ iplist [0] = 10)
Return 'private network [intranet] ';
If ($ iplist [0] = 0)
Return 'This network (only valid as the source address )';
Return 'Internet address ';
}
When you enter an IP address and it returns "InterNet address", this IP address is not only in correct format, but also a valid IP address on the InterNet. This function is very complex. In fact, it is to exclude many non-Internet IP addresses. We are familiar with the common 192,127 and 10 addresses. But in fact, many IP addresses are retained or reserved for use. Cannot be used as an Internet IP address. With the above two functions, we can not only read the IP addresses in the correct format, but also ensure that the IP addresses on the Internet are read. The above are commonly used functions at work. You are welcome to exchange them!
Author: chengmo QQ: 8292669
The callback code is as follows: function getIP () {$ realip = ''; // you can specify the default value if (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']). {$ realip = $ _ SERVER ['http _...