login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > IP range vs IP check

IP range vs IP check

Auteur: Paul Gregg - 29 april 2009 - 14:19 - Gekeurd door: Koen - Hits: 4158 - Aantal punten: (0 stemmen)



Bekijk of een ip adres voorkomt in een bepaalde range

Code:
  1. <?php
  2. // decbin32
  3. // In order to simplify working with IP addresses (in binary) and their
  4. // netmasks, it is easier to ensure that the binary strings are padded
  5. // with zeros out to 32 characters - IP addresses are 32 bit numbers
  6. Function decbin32 ($dec) {
  7. return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
  8. }
  9.  
  10. // ip_in_range
  11. // This function takes 2 arguments, an IP address and a "range" in several
  12. // different formats.
  13. // Network ranges can be specified as:
  14. // 1. Wildcard format: 1.2.3.*
  15. // 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
  16. // 3. Start-End IP format: 1.2.3.0-1.2.3.255
  17. // The function will return true if the supplied IP is within the range.
  18. // Note little validation is done on the range inputs - it expects you to
  19. // use one of the above 3 formats.
  20. Function ip_in_range($ip, $range) {
  21. if (strpos($range, '/') !== false) {
  22. // $range is in IP/NETMASK format
  23. list($range, $netmask) = explode('/', $range, 2);
  24. if (strpos($netmask, '.') !== false) {
  25. // $netmask is a 255.255.0.0 format
  26. $netmask = str_replace('*', '0', $netmask);
  27. $netmask_dec = ip2long($netmask);
  28. return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  29. } else {
  30. // $netmask is a CIDR size block
  31. // fix the range argument
  32. $x = explode('.', $range);
  33. while(count($x)<4) $x[] = '0';
  34. list($a,$b,$c,$d) = $x;
  35. $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
  36. $range_dec = ip2long($range);
  37. $ip_dec = ip2long($ip);
  38.  
  39. # Strategy 1 - Using substr to chop up the range and pad it with 1s to the right
  40. $broadcast_dec = bindec(substr(decbin32($range_dec), 0, $netmask)
  41. . str_pad('', 32-$netmask, '1'));
  42.  
  43. # Strategy 2 - Use math to OR the range with the wildcard to create the Broadcast address
  44. $wildcard_dec = pow(2, (32-$netmask)) - 1;
  45. $broadcast_dec = $range_dec | $wildcard_dec;
  46.  
  47. return (($ip_dec & $broadcast_dec) == $ip_dec);
  48. }
  49. } else {
  50. // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  51. if (strpos($range, '*') !==false) { // a.b.*.* format
  52. // Just convert to A-B format by setting * to 0 for A and 255 for B
  53. $lower = str_replace('*', '0', $range);
  54. $upper = str_replace('*', '255', $range);
  55. $range = "$lower-$upper";
  56. }
  57.  
  58. if (strpos($range, '-')!==false) { // A-B format
  59. list($lower, $upper) = explode('-', $range, 2);
  60. $lower_dec = ip2long($lower);
  61. $upper_dec = ip2long($upper);
  62. $ip_dec = ip2long($ip);
  63. return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
  64. }
  65.  
  66. echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
  67. return false;
  68. }
  69.  
  70. $ip_dec = ip2long($ip);
  71. printf("ip_dec = %s\n", decbin32($ip_dec));
  72. printf("netmask_dec = %s\n", decbin32($netmask_dec));
  73. printf("ANDed = %s\n", decbin32(($ip_dec & $netmask_dec)));
  74. return (($ip_dec & $netmask_dec) == $ip_dec);
  75. }
  76. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (2)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.028s