web-dev-qa-db-fra.com

Comment vérifier qu'une adresse IP est comprise entre deux adresses IP en PHP?

J'ai une adresse IP et on me donne deux autres adresses IP qui créent ensemble une plage IP. Je veux vérifier si la première adresse IP est dans cette plage. Comment puis-je trouver cela en PHP?

46
guitarlass

Avec ip2long() il est facile de convertir vos adresses en nombres. Après cela, il vous suffit de vérifier si le nombre est dans la plage:

if ($ip <= $high_ip && $low_ip <= $ip) {
  echo "in range";
}
58
oezi

Ce site propose un excellent guide et code pour ce faire (premier résultat d’une recherche Google sur cette question):

<?php

/*
 * ip_in_range.php - Function to determine if an IP is located in a
 *                   specific range as specified via several alternative
 *                   formats.
 *
 * Network ranges can be specified as:
 * 1. Wildcard format:     1.2.3.*
 * 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
 * 3. Start-End IP format: 1.2.3.0-1.2.3.255
 *
 * Return value BOOLEAN : ip_in_range($ip, $range);
 *
 * Copyright 2008: Paul Gregg <[email protected]>
 * 10 January 2008
 * Version: 1.2
 *
 * Source website: http://www.pgregg.com/projects/php/ip_in_range/
 * Version 1.2
 *
 * This software is Donationware - if you feel you have benefited from
 * the use of this tool then please consider a donation. The value of
 * which is entirely left up to your discretion.
 * http://www.pgregg.com/donate/
 *
 * Please do not remove this header, or source attibution from this file.
 */


// decbin32
// In order to simplify working with IP addresses (in binary) and their
// netmasks, it is easier to ensure that the binary strings are padded
// with zeros out to 32 characters - IP addresses are 32 bit numbers
Function decbin32 ($dec) {
  return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
}

// ip_in_range
// This function takes 2 arguments, an IP address and a "range" in several
// different formats.
// Network ranges can be specified as:
// 1. Wildcard format:     1.2.3.*
// 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
// The function will return true if the supplied IP is within the range.
// Note little validation is done on the range inputs - it expects you to
// use one of the above 3 formats.
Function ip_in_range($ip, $range) {
  if (strpos($range, '/') !== false) {
    // $range is in IP/NETMASK format
    list($range, $netmask) = explode('/', $range, 2);
    if (strpos($netmask, '.') !== false) {
      // $netmask is a 255.255.0.0 format
      $netmask = str_replace('*', '0', $netmask);
      $netmask_dec = ip2long($netmask);
      return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
    } else {
      // $netmask is a CIDR size block
      // fix the range argument
      $x = explode('.', $range);
      while(count($x)<4) $x[] = '0';
      list($a,$b,$c,$d) = $x;
      $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
      $range_dec = ip2long($range);
      $ip_dec = ip2long($ip);

      # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
      #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));

      # Strategy 2 - Use math to create it
      $wildcard_dec = pow(2, (32-$netmask)) - 1;
      $netmask_dec = ~ $wildcard_dec;

      return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
    }
  } else {
    // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
    if (strpos($range, '*') !==false) { // a.b.*.* format
      // Just convert to A-B format by setting * to 0 for A and 255 for B
      $lower = str_replace('*', '0', $range);
      $upper = str_replace('*', '255', $range);
      $range = "$lower-$upper";
    }

    if (strpos($range, '-')!==false) { // A-B format
      list($lower, $upper) = explode('-', $range, 2);
      $lower_dec = (float)sprintf("%u",ip2long($lower));
      $upper_dec = (float)sprintf("%u",ip2long($upper));
      $ip_dec = (float)sprintf("%u",ip2long($ip));
      return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
    }

    echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
    return false;
  }

}
?>
30
John Conde

J'ai trouvé ce petit Gist Qui a une solution plus simple/plus courte que celle déjà mentionnée ici. 

Le deuxième argument (plage) peut être une adresse IP statique telle que 127.0.0.1 ou une plage telle que 127.0.0.0/24.

/**
 * Check if a given ip is in a network
 * @param  string $ip    IP to check in IPV4 format eg. 127.0.0.1
 * @param  string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
 * @return boolean true if the ip is in this range / false if not.
 */
function ip_in_range( $ip, $range ) {
    if ( strpos( $range, '/' ) == false ) {
        $range .= '/32';
    }
    // $range is in IP/CIDR format eg 127.0.0.1/24
    list( $range, $netmask ) = explode( '/', $range, 2 );
    $range_decimal = ip2long( $range );
    $ip_decimal = ip2long( $ip );
    $wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
    $netmask_decimal = ~ $wildcard_decimal;
    return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}
11
codefreak
if(version_compare($low_ip, $ip) + version_compare($ip, $high_ip) === -2) {
    echo "in range";
}
2
Bas

Je suggérerais toujours ip2long , mais vous devez parfois vérifier les réseaux, etc. J'ai déjà construit une classe de réseau IPv4, que vous pouvez trouver ici sur HighOnPHP .

Le bon côté de l’adressage IP est sa flexibilité, en particulier lors de l’utilisation d’opérateurs BITWISE. AND'ing, OR'ing et BitShifting fonctionneront à merveille.

2
Mike Mackintosh

Voici mon approche du sujet.

function validateIP($whitelist, $ip) {

    // e.g ::1
    if($whitelist == $ip) {
        return true;
    }

    // split each part of the IP address and set it to an array
    $validated1 = explode(".", $whitelist);
    $validated2 = explode(".", $ip);

    // check array index to avoid undefined index errors
    if(count($validated1) >= 3 && count($validated2) == 4) {

        // check that each value of the array is identical with our whitelisted IP,
        // except from the last part which doesn't matter
        if($validated1[0] == $validated2[0] && $validated1[1] == $validated2[1] && $validated1[2] == $validated2[2]) {
            return true;
        }   

    }

    return false;
}
0
user8581607

Btw, au cas où vous auriez besoin de vérifier plusieurs plages à la fois, vous pouvez ajouter quelques lignes au code afin de passer un tableau de plages. Le deuxième argument peut être un tableau ou une chaîne:

public static function ip_in_range($ip, $range) {
      if (is_array($range)) {
          foreach ($range as $r) {
              return self::ip_in_range($ip, $r);
          }
      } else {
          if ($ip === $range) { // in case you have passed a static IP, not a range
             return TRUE;
          }
      } 
      // The rest of the code follows here..
      // .........
}
0
Vlado

Comparaison dans la plage (incluant le support Ipv6)

Les deux fonctions suivantes ont été introduites dans PHP 5.1.0, inet_pton et inet_pton. Leur but est de convertir les adresses IP lisibles par l'homme en leur représentation empreinte in_addr. Comme le résultat n'est pas purement binaire, nous devons utiliser la fonction unpack pour appliquer des opérateurs au niveau du bit.

Les deux fonctions prennent en charge IPv6 et IPv4. La seule différence est la façon dont vous décompressez l'adresse à partir des résultats. Avec IPv6, vous décompressez avec le contenu avec A16 et avec IPv4, vous décompressez avec A4.

Pour mettre le précédent dans une perspective, voici un petit exemple de sortie pour aider à clarifier:

// Our Example IP's
$ip4= "10.22.99.129";
$ip6= "fe80:1:2:3:a:bad:1dea:dad";


// ip2long examples
var_dump( ip2long($ip4) ); // int(169239425)
var_dump( ip2long($ip6) ); // bool(false)


// inet_pton examples
var_dump( inet_pton( $ip4 ) ); // string(4)
var_dump( inet_pton( $ip6 ) ); // string(16)

Nous démontrons ci-dessus que la famille inet_ * prend en charge IPv6 et v4. Notre prochaine étape consistera à traduire le résultat condensé en une variable décompactée.

// Unpacking and Packing
$_u4 = current( unpack( "A4", inet_pton( $ip4 ) ) );
var_dump( inet_ntop( pack( "A4", $_u4 ) ) ); // string(12) "10.22.99.129"


$_u6 = current( unpack( "A16", inet_pton( $ip6 ) ) );
var_dump( inet_ntop( pack( "A16", $_u6 ) ) ); //string(25) "fe80:1:2:3:a:bad:1dea:dad"

Remarque: La fonction actuelle renvoie le premier index d'un tableau. Cela équivaut à dire $ array [0].

Après le déballage et l’emballage, nous pouvons constater que nous avons obtenu le même résultat que celui entré. Il s’agit d’une simple validation de principe qui garantit que nous ne perdons aucune donnée.

Enfin utiliser,

if ($ip <= $high_ip && $low_ip <= $ip) {
  echo "in range";
}

Référence: php.net

0
Sazzad Hissain Khan