Hello!

I have a db with IP ranges which are stored in long format, having been entered using ip2long function. My problem is that when I edit a range over 127.255.255.255, the range is put out of numerical order and if I search for an IP in a range out of numerical order, the range is no longer seen. For grins, I entered the IP range of 222.240.0.0 thru 222.247.255.255 into the db, which stored as 3740270592 and 3740794879. I edited the range and checked the db record, and it changed to -554696704 and -554172417! The IP appears to be the same when converted with long2ip function, but clearly it is not. The problem does not occur when the IP is 127.255.255.255 or less.

My db uses the bigint datatype for the variables $sip and $eip. I tried changing it to int unsigned, which did not work out and actually caused some peculiar behavior. The fact that edits work fine in the 0.0.0.0 to 127.255.255.255 range but not above makes me think that the code is somehow flawed, but I am still new to PHP and unsure how to troubleshoot this complicated problem (complicated to me anyways!)

Could someone please take a peek at my edit script and tell me where I am erring? Thank you in advance 🙂

<?PHP

session_start();
if(!session_is_registered(user)){
header("location:login.php");
}
include  ("config.php");

// $string is just a placeholder
function escapeSingleQuotes($string){
//escapse single quotes
$singQuotePattern = "'";
$singQuoteReplace = "''";
return(stripslashes(eregi_replace($singQuotePattern, $singQuoteReplace, $string)));
}

$x = split("\.",$newstart);
$sip = (256*256*256*$x[0]) + (256*256*$x[1]) + (256*$x[2]) + ($x[3]);

$y = split("\.",$newend);
$eip = (256*256*256*$y[0]) + (256*256*$y[1]) + (256*$y[2]) + ($y[3]);

echo long2ip($sip);


 if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'update')
{
        $id = $_REQUEST['id'];
        $_GET['id'] = $id;
        $query = "UPDATE ips SET  sip = '".ip2long($_REQUEST['newstart'])."', eip = '".ip2long($_REQUEST['newend'])."', contact = '".escapeSingleQuotes($_REQUEST['newcontact'])."', phone = '".escapeSingleQuotes($_REQUEST['newphone'])."', e1 = '".escapeSingleQuotes($_REQUEST['newe1'])."' WHERE id='$id'";
        $result = mysql_query($query) or die("<b>mySQL Error:</b>");
        if(!$result)
        {
            echo 'Error processing request.';
        }
        else
        {
            echo '<B>The Record has been successfully updated!</B>';
        }
    }

$id = $_GET['id'];
// The ID is passed through the URL to specify the row,
// Or it is set in the previous script.

$query = "SELECT * FROM ips WHERE id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


echo '
<form name="update1" method="post">
        <input type="hidden" value="update" name="action">
        <input type="hidden" name="id" value="'.$id.'">

<table class="menu" align="center">

<tr>
    <th colspan="2">Edit IP Block</th>
</tr>

<tr>
    <td class="selcol1">Starting IP Address:</td>
	<td class="selcol2"><input class="white" type="text" name="newstart" size="50" value="'.long2ip($row['sip']).'" tabindex="10"></td>
</tr>

<tr>
    <td class="selcol1">Ending IP Address:</td>
	<td class="selcol2"><input class="white" type="text" name="newend" size="50" value="'.long2ip($row['eip']).'" tabindex="20"></td>
</tr>

<tr>
    <td class="selcol1">Contact:</td>
	<td class="selcol2"><input class="white" type="text" name="newcontact" size="50" value="'.$row['contact'].'" tabindex="60"></td>
</tr>

<tr>
    <td class="selcol1">Phone:</td>
	<td class="selcol2"><input class="white" type="text" name="newphone" size="50" value="'.$row['phone'].'" tabindex="70"></td>
</tr>

<tr>
    <td class="selcol1">Email:</td>
	<td class="selcol2"><input class="white" type="text" name="newe1" size="50" value="'.$row['e1'].'" tabindex="80"></td>
</tr>

<tr>
    <td class="selcol1">Alternate Email:</td>
	<td class="selcol2"><input class="white" type="text" name="newe2" size="50" value="'.$row['e2'].'" tabindex="90"></td>
</tr>

    From http://www.php.net/ip2long :

    Note: Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address.

    $query = "UPDATE ips SET  sip = '".sprintf("%u",ip2long($_REQUEST['newstart']))."',...";
    

      There I go again using a function I hadn't completely understood 🙂

      Thank you NogDog!

        Write a Reply...