This article introduces two functions one is to verify the IP address, the other is to obtain the user's real IP address of the function, is also commonly used two IP operation function, the need for students can refer to.
1. Get the user's real IP address
function get_client_ip () {if (getenv ("REMOTE_ADDR") && STRCASECM P (getenv ("REMOTE_ADDR"), "Unknown")) {$onlineip = getenv ("REMOTE_ADDR"); return $ONLINEIP; } if (Isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' REMOTE _addr '], "unknown") {$onlineip = $_server[' remote_addr ']; return $ONLINEIP; } if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "unknown") { $onlineip = getenv ("Http_client_ip"); return $ONLINEIP; } if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")) {$onlineip = getenv ("Http_x_forwarded_for"); } return $ONLINEIP; }
Determine if an IP address
function Is_ip ($IP) { $IP _array = Explode (".", $IP); $IP _array_num = sizeof ($IP _array); if ($IP _array_num! = 4) { return FALSE; } $I = 0; for (; $I < $IP _array_num; + + $I ) { if (!is_numeric ($IP _array[$I]) && $IP _array[$I] < 0 | | 255 < $IP _array[$I])
{ return FALSE; } if (! ($I = = 3) &&! ($IP _array[$I] = = 255)) { continue; } return FALSE; } return TRUE; }
Related articles:
PHP Verification of ID number correctness function
PHP ID Number Verification function
Three php form validation functions commonly used in project development