php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14018 make ip2long and long2ip handle unsigned longs correctly
Submitted: 2001-11-11 13:15 UTC Modified: 2001-12-07 08:32 UTC
From: jan at kneschke dot de Assigned:
Status: Closed Package: Network related
PHP Version: 4.0.6 OS: Linux
Private report: No CVE-ID: None
 [2001-11-11 13:15 UTC] jan at kneschke dot de
Actually the my fix for this problem is on the PHP side, 
not in the PHP-code it self:


function jk_ip2long ($ip) {
    
preg_match("#^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$#", 
$ip, $a);

    $long = 
      $a[1] << 24 | 
      $a[2] << 16 |
      $a[3] <<  8 |
      $a[4] <<  0;
    
    ## handling negative urls
    if($long < 0) $long += pow(2,32); 
    
    return $long;
}

## long2ip

function jk_long2ip ($long) {
    ## handle long value which should be signed in PHP but 
are unsigned
    
    if ($long > 0x7fffffff) {
        ## although this code seems to be useless, it does 
what we want
        $l = ($long - 0x7fffffff) - 1;
        $long = -(0x7fffffff - $l) - 1;
    }
    
    
    $ip3 = sprintf("%d.%d.%d.%d",
                   $long >> 24 & 0xff,
                   $long >> 16 & 0xff,
                   $long >>  8 & 0xff,
                   $long >>  0 & 0xff
                   );
    return $ip3;
}

You can see a working example at

http://jan.kneschke.de/projects/phpbugs/ip2long.php


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-07 08:32 UTC] sterling@php.net
They work in the current CVS of php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC