Jump to content

IP Validation and ip2long, long2ip


BadGoat

Recommended Posts

[quote]The weird anomaly where an edited record is put out of numerical order once an IP address is changed is my next (and last) problem..[/quote]

Modify the database query to add something like "ORDER by (any table field name) ASC" if you want ASCending order or DESC is you want DESCending order in the returned results.
Link to comment
Share on other sites

Hi Andy,

I have a ORDER BY in my MySQL query, so I didn't think it was that. I did some research... For grins, I added an IP range of 222.240.0.0 to 222.247.255.255 to my db... Converted using ip2long, it is entered into the db as 3740270592 ($sip, which is 222.240.0.0) and 3740794879 ($eip, which is 222.247.255.255). If I edit the range, it is changed in the db as -554696704 ($sip, which is 222.240.0.0) and -554172417 ($eip, which is 222.247.255.255). When converted back to IP with long2ip, it still shows the correct IP address, it is just not listed in correct numerical order. Is it possible that it is being double-converted with ip2long? Or is it possible that the portion of code which splits the code is not being used due to a flaw in the logic? Here's the pertinent code:

[code]
$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]);

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'])."' 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>[/code]

I've tinkered with it quite a bit, changing things, but the only thing I have accomplished is confusing myself even more.
Link to comment
Share on other sites

Pardon the bump, I should note that it works perfectly for IP addresses below the 127.255.255.255 range, but anything over 128.0.0.0 is put ourt of numerical order.  I swear I saw something on another PHP site about 32-bit vs.64-bit or something which caused the problem, but I am not certain, nor can I find that page again...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.