php限制ip地址范围的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了php限制ip地址范围的方法。分享给大家供大家参考。具体如下:

只有在限定范围内的ip地址才能访问

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: //www.jb51.net');
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

php写入txt乱码的解决方法

相信很多人在使用PHP的过程中都遇到过生成文件乱码的情况,不论是使用fwrite还是file_put_contents写入。可能你会先尝试从编码入手尝试解决,但最终的结果往往是不理想的,...

PHP syntax error, unexpected $end 错误的一种原因及解决

Parse error: syntax error, unexpected $end in script.php on line xx 调试了一会后发现产生错误的行是文件中间某行 //$...

PHP基于递归算法解决兔子生兔子问题

本文实例讲述了PHP基于递归算法解决兔子生兔子问题。分享给大家供大家参考,具体如下: 接到面试通知辗转反侧,一直在默念明天改如何介绍自己的项目经验等。 早早的起床,洗漱,把自己的总结的问...

PHP中file_exists与is_file,is_dir的区别介绍

很显然file_exists是受了asp的影响,因为asp不但有fileExists还有folderExists,driverExists,那么PHP中file_exists是什么意思呢...

php解析base64数据生成图片的方法

php解析base64数据生成图片的方法

本文实例讲述了php解析base64数据生成图片的方法。分享给大家供大家参考,具体如下: $base64 = "/9j/4AAQSkZJRgABAQEAkACQAAD/4QCMRXh...