Search code examples
phpsortingip

Merge IP Ranges with PHP


I am stuck at a point and cant think beyond it :

I have a list of Ip ranges, I need to merge them making them as a range. For example

192.168.1.1

192.168.1.2

192.168.1.3

192.168.1.5

192.168.1.6

I need to club them into IP ranges. For this case it will be 192.168.1.1-192.168.1.3 and 192.168.1.5-192.168.1.6

I was thinking of converting these into integer by using ip2long function and sorting it. But can't think beyond that.

Any help would be appreciated.

Thanks In advance :)


Solution

  • you can use ip2long http://php.net/manual/en/function.ip2long.php that Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address with this you can have a range like

    echo ip2long('192.168.1.1');//-1062731519
    
    echo ip2long('192.168.1.6');//-1062731514
    

    with this you can check ip range of user ,... for valid or ,...

    for example if user ip is in 192.168.1.1-192.168.1.6 range echo 'hello';

    //http://www.php.net/manual/en/function.ip2long.php#81030
    function in_ip_range($ip_one, $ip_two=false){ 
      if($ip_two===false){ 
        if($ip_one==$_SERVER['REMOTE_ADDR']){ 
            $ip=true; 
        }else{ 
            $ip=false; 
        } 
      }else{ 
        if(ip2long($ip_one)<=ip2long($_SERVER['REMOTE_ADDR']) && ip2long($ip_two)>=ip2long($_SERVER['REMOTE_ADDR'])){ 
            $ip=true; 
        }else{ 
            $ip=false; 
        } 
      } 
      return $ip; 
     } 
     //usage 
     if(in_ip_range('192.168.1.1','192.168.1.6')){
        echo 'hello';
     }
    

    ip group

      $list=array(
       '192.168.1.1',
       '192.168.1.2',
    
       '192.168.1.3',
    
       '192.168.1.5',
    
       '192.168.1.6',
       '192.168.1.6',
       '192.168.1.9'
     );
     $Grouplist='';
     foreach($list as $ip){ 
           $ip2long=ip2long($ip);
           if(is_array($Grouplist)){
                $is_group=false;
                foreach($Grouplist as $Key=>$Range){
                     $Range=explode("/",$Range);
                     if(($Range[0]-1)<$ip2long and $ip2long<($Range[1]+1)){
                           $is_group=true;
                           continue;
                     }elseif(($Range[0]-1)==$ip2long){
                           $Grouplist[$Key]=$ip2long.'/'.$Range[1];
                           $is_group=true;
                     }elseif(($Range[1]+1)==$ip2long){
                          $Grouplist[$Key]=$Range[0].'/'.$ip2long;
                          $is_group=true;
                     }
                }
                if(!$is_group)
                {
                      $Grouplist[]=($ip2long).'/'.($ip2long);
                }
           }else{
                 $Grouplist[]=($ip2long).'/'.($ip2long);
           }
     }
         print_r($Grouplist); 
    

    output:

     Array
    (
       [0] => -1062731519/-1062731517  
       [1] => -1062731515/-1062731514
       [2] => -1062731511/-1062731511
    )
    

    that if convert to ip group is

    foreach($Grouplist as $Val){
         $Range=explode("/",$Val);
         echo long2ip($Range[0])."/".long2ip($Range[1])."\n";
    }
    

    output

    192.168.1.1/192.168.1.3
    192.168.1.5/192.168.1.6
    192.168.1.9/192.168.1.9