检测ip是否在一个指定范围网段里面,如果在里面返回true,不存在返回false和msg错误信息

代码:

  1. <?php
  2. /**
  3. * 说明:检测IP是否在IP段内
  4. * 作者:upall
  5. */
  6. class ipCheck {
  7. public $ipRangeStr = '10.0.0.1/8';
  8. public $msg = '';
  9.  
  10. function __construct($ipRangeStr){
  11. !empty($ipRangeStr) ? $this->ipRangeStr = $ipRangeStr : '';
  12. }
  13.  
  14. function check($ip = '') {
  15. empty($ip) && $ip = $this->getClientIp();
  16. # 判断检测类型
  17. if (FALSE !== strpos($this->ipRangeStr,'-')){
  18. $type = 'size'; // 简单比大小:10.0.0.1-254 OR 10.0.0.1-10.0.0.254
  19. }else if(FALSE !== strpos($this->ipRangeStr,'/')){
  20. $type = 'mask'; // 掩码比大小:10.0.0.1/24
  21. }else{
  22. $this->msg = '错误的IP范围值';
  23. return FALSE;
  24. }
  25. # 分析IP范围
  26. if ('size' === $type){
  27. $ipRangeStr = explode('-',$this->ipRangeStr);
  28. $ipAllowStart = $ipRangeStr[0];
  29. $ipAllowEnd = $ipRangeStr[1];
  30. if (FALSE === strpos($ipAllowEnd,'.')){ # 10.0.0.254 OR 254
  31. $ipAllowElmArray = explode('.',$ipAllowStart);
  32. $ipAllowEnd = $ipAllowElmArray[0] . '.' .
  33. $ipAllowElmArray[1] . '.' .
  34. $ipAllowElmArray[2] . '.' .
  35. $ipAllowEnd;
  36. }
  37. }else if ('mask' === $type){
  38. $ipRangeStr = explode('/',$this->ipRangeStr);
  39. $ipRangeIP = $ipRangeStr[0];
  40. # 获取掩码中最后一位非零数的值
  41. $ipRangeMask = (int)$ipRangeStr[1];
  42. $maskElmNumber = floor($ipRangeMask/8); # 保留IP前几段
  43. $maskElmLastLen = $ipRangeMask % 8; # 255.255.here.0
  44. $maskElmLast = str_repeat(1,8-$maskElmLastLen);
  45. $maskElmLast = bindec($maskElmLast); # 掩码中IP末段最大值(十进制)
  46. // 获取IP段开始、结束值
  47. $ipRangeIPElmArray = explode('.',$ipRangeIP);
  48. if (0 == $maskElmNumber){
  49. $ipAllowStart = '0.0.0.0';
  50. $ipAllowEnd = $maskElmLast . '.254.254.254';
  51. }else if (1 == $maskElmNumber){
  52. $ipAllowStart = $ipRangeIPElmArray[0] . '.' . '0.0.0';
  53. $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $maskElmLast . '.254.254';
  54. }else if (2 == $maskElmNumber){
  55. $ipAllowStart = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . '0.0';
  56. $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $maskElmLast . '.254';
  57. }else if (3 == $maskElmNumber){
  58. $ipAllowStart = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $ipRangeIPElmArray[2] . '.' . '0';
  59. $ipAllowEnd = $ipRangeIPElmArray[0] . '.' . $ipRangeIPElmArray[1] . '.' . $ipRangeIPElmArray[2] . '.' . $maskElmLast;
  60. }else if (4 == $maskElmNumber){
  61. $ipAllowEnd = $ipAllowStart = $ipRangeIP;
  62. }else{
  63. $this->msg = '错误的IP段数据';
  64. return $this->msg;
  65. }
  66. }else{
  67. $this->msg = '错误的IP段类型';
  68. return $this->msg;
  69. }
  70. // 检测IP
  71. $ipAllowStart = $this->getDecIp($ipAllowStart);
  72. $ipAllowEnd = $this->getDecIp($ipAllowEnd);
  73. $ip = $this->getDecIp($ip);
  74. if (!empty($ip)){
  75. if ($ip <= $ipAllowEnd && $ip >= $ipAllowStart){
  76. $this->msg = 'IP检测通过';
  77. return TRUE;
  78. }else{
  79. $this->msg = '此为被限制IP';
  80. return FALSE;
  81. }
  82. }else{
  83. FALSE === ($this->msg) && $this->msg == '没有提供待检测IP'; // getClentIp() 是否返回false
  84. return $this->msg; // 没有获取到客户端IP,返回
  85. }
  86. }
  87.  
  88. // 10进制IP
  89. function getDecIp($ip){
  90. $ip = explode(".", $ip);
  91. return $ip[0]*255*255*255+$ip[1]*255*255+$ip[2]*255+$ip[3];
  92. }
  93.  
  94. // 获取客户端IP
  95. function getClientIp(){
  96. if(isset($_SERVER['REMOTE_ADDR'])){
  97. return $_SERVER['REMOTE_ADDR'];
  98. }else{
  99. $this->msg = '不能获取客户端IP';
  100. return FALSE;
  101. }
  102. }
  103. }
  104. ?>

用法示例

  1. $ipCheck = new ipCheck('192.168.1.1-192.168.1.254');
  2. echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;
  3.  
  4. $ipCheck = new ipCheck('192.168.1.1-254');
  5. echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;
  6.  
  7. $ipCheck = new ipCheck('192.168.1.1/24');
  8. echo (TRUE === $ipCheck ->check('192.168.1.45')) ? '在范围内' : $ipCheck->msg;

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!