Jump to content


Photo

IP Validation and ip2long, long2ip


  • Please log in to reply
27 replies to this topic

#21 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 06:26 PM

Andy,
That got rid of the mysql error. Punctuation is gonna be my friend, kicking and screaming. ;)
As for the output: When I type an IP I know is within a range in my db, I get the error: "Sorry, but we can not find an entry to match your query" as if it does not recognize.

#22 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 06:48 PM

OK, so we're making progress.  The reason why you see that message is .... either the ip really is not in the database or the logic for generating that message is flawed.

Step 1 would be to echo the query (that results in finding nothing).  So, modify the code I gave you to add:

echo $query. "<br/>";

between the query definition and the $result = line. If you then copy/paste that same query into your database administration tool (phpMyAdmin if you're lucky) and see what results.  If you find nothing then the next step should be obvious. If you get one or more results, then your logic is flawed.

Perhaps as we're so close to success (although it might not feel like it), now is the time to post a fairly complete copy of your code as it exists right now. The input form as well as the database query script.
Legend has it that reading the manual never killed anyone.
My site

#23 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 07:10 PM

The query, as echoed, when searching for 11.0.0.1 (the range for which I added for S&Gs): SELECT * FROM ips WHERE sip > '184615169' AND eip < '184615169'

Using PHPMyAdmin, I go to the range and check, and within, sip is '184549376'  and eip is '201326591'.

And by looking at this, I figured that the query should be
$query = "SELECT * FROM ips WHERE sip < '$find' AND eip > '$find'"; // exactly like this

And with this, I do not get the "Sorry, but we can not find an entry to match your query" error anymore...

Even more progress!

One of my final problems is figuring out how to echo other information which is stored in the same record... Such as the contact name ($contact) phone #($phone) and email address($e1).

***EDIT***


I just fixed that problem. Without help.. I surprised myself... hehehe

There is only one problem left... When I edit an IP range the edited IP range is listed out of numerical order once I touch one of the IP addresses. Here's my edit.php script:
<?PHP

session_start();
if(!session_is_registered(user)){
header("location:login.php");
}
include  ("config.php");
echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Edit IP Block</title>
<link rel=stylesheet href="css.css" type="text/css">
</head>
<body>';

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

 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'])."', nic = '".escapeSingleQuotes($_REQUEST['newnic'])."', 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>

<tr>
    <td class="selcol1">Email Verified:</td>
    <td class="selcol2"><select name="newe_ver" tabindex="30">
        <option value="'.$row['e_ver'].'">'.$row['e_ver'].'</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>

    </select></td>
</tr>     ';
?>

<input type="hidden" name="newupdated" value="<? date('M j Y')?>">

<tr>
    <td class="header" colspan="2"><center><input class="blue" type="submit" value="Update" action="required" tabindex="110"><br><br>
        </form>
        </td>
</tr>

<tr>
    <td class="spacer" colspan="2">&nbsp;</td>
</tr>

</TABLE>
</BODY>
</HTML>

And the script which lists the IP ranges in numerical order:
<?PHP

session_start();
if(!session_is_registered(user)){
header("location:login.php");
}
include  ("config.php");
echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>List IP Blocks</title>
<link rel=stylesheet href="css.css" type="text/css">
</head>
<body>';

$sqlquery = "SELECT id, sip, eip, nic, e1, contact, phone FROM  ips ORDER BY sip";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query !");
$row = mysql_fetch_row($queryresult);

$id = $row[0];
$sip = $row[1];
$eip = $row[2];
$contact = $row[3];
$phone = $row[4];
$e1 = $row[5];

$tdcount = 1;
$numtd = 1; // number of cells per row
echo '

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

<tr>
    <th colspan="6">List IP Blocks</th>
</tr>

<tr>
    <td class="selcol3">ID</td>
    <td class="selcol3">IP Block Start</td>
    <td class="selcol3">IP Block End</td>
    <td class="selcol3">Contact</td>
    <td class="selcol3">Phone</td>
    <td class="selcol3">Email</td>
</tr>

<TR>  ';
   mysql_data_seek($queryresult,0)  ;
while($row = mysql_fetch_array($queryresult)) {

    if ($tdcount == 1) echo "<tr>";
echo '
    <td class="selcol2"><a class="style1" href="ip_block_view.php?id='.$row['id'].'"> '.$row['id'],'</a></td>
    <td class="selcol2"> '.long2ip($row['sip']),'</td>
    <td class="selcol2"> '.long2ip($row['eip']),'</td>
    <td class="selcol2"> '.$row['contact'],'</td>
    <td class="selcol2"> '.$row['phone'],'</td>
    <td class="selcol2"> '.$row['e1'],'</td>
    ';
 if ($tdcount == $numtd) {
 echo "</tr>";
 $tdcount = 1;
 } else {
 $tdcount++;
 }

 echo "</tr>";
}
echo '

</table> ';
?>

</BODY>
</HTML>




#24 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 07:27 PM

doh! That was my fault for getting the start and end reversed.

as to how you output other data from the results, add more output in the while loop ...

echo $result['contact']; // show contact name

See you've worked that one out.
Legend has it that reading the manual never killed anyone.
My site

#25 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 07:37 PM

Andy: A sincere THANK YOU for putting up with my goatheadedness and assisting me through my issues!

Getting the rest of the record turned out to be pretty easy:
    $query = "SELECT * FROM ips WHERE sip < '$find' AND eip > '$find'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
echo'
<br>
<tr>
    <td class="selcol1">Contact</td>
    <td class="selcol2">'.$row['contact'].'</td>
</tr>

<tr>
    <td class="selcol1">Phone</td>
    <td class="selcol2">'.$row['phone'].'</td>
</tr>

<tr>
    <td class="selcol1">Email</td>
    <td class="selcol2">'.$row['e1'].'</td>
</tr>

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..

#26 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 08:03 PM

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..


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.
Legend has it that reading the manual never killed anyone.
My site

#27 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 30 August 2006 - 06:15 PM

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:

$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>

I've tinkered with it quite a bit, changing things, but the only thing I have accomplished is confusing myself even more.

#28 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 30 August 2006 - 07:18 PM

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...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users