Home > Article > Backend Development > How to limit the IP address range in php_PHP tutorial
Only IP addresses within the limited range can be accessed
3 4
5
146 13 |
<๐> <๐>function get_real_ipaddress() {<๐> <๐>if (!empty($_SERVER['HTTP_CLIENT_IP'])) {<๐> <๐>return $_SERVER['HTTP_CLIENT_IP'];<๐> <๐>} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {<๐> <๐>return $_SERVER['HTTP_X_FORWARDED_FOR'];<๐> <๐>}<๐> <๐>return $_SERVER['REMOTE_ADDR'];<๐> <๐>}<๐> <๐>function in_ip_range($ip, $ip_one, $ip_two = false) {<๐> <๐>if(!$ip_two) {<๐> <๐>return $ip_one === $ip;<๐> <๐>}<๐> <๐>return ip2long($ip_one) * -1 >= ip2long($ip) * -1 && ip2long($ip_two) * -1 <= ip2long($ip) * -1; } function validate_ip() { $ip = explode(':', get_real_ipaddress()); $ip = $ip[0]; if(in_ip_range($ip, '212.76.229.115', '212.76.229.120')) { return true; } else if(in_ip_range($ip, '194.78.4.66', '194.78.4.79')) { return true; } else if(in_ip_range($ip, '194.8.4.78', '194.8.4.78')) { return true; } else if(in_ip_range($ip, '0', '1')) { // local return true; } header('Location: http://www.bkjia.net'); } |