Forum Moderators: coopster

Message Too Old, No Replies

Problem searching for an IP address between two ranges

both of which are stored in long format

         

BadGoat

4:12 pm on Aug 29, 2006 (gmt 0)

10+ Year Member



Hello!

I have a script which stores ranges of IP addresses which I save to the db using ip2long function. (because storing them as a string creates funny problems when trying to list them numerically)The script to add the IPs to the db works fine, displaying the IPs back from long format to IP address using long2ip function works fine. The problem I am having is trying to search the db by entering an IP address and having the db record which has that IP within it's range displayed.

Were I to guess, I would say that the problem lies in the translation of the searched IP being translated to long form, but I'm a nooblet and unsure.

As currently coded, I am getting an error, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '> 11.1.1.1 AND < 11.1.1.1' at line 1" which changes to the long form of the 11.x.x.x IP address, depending on if I comment out two lines in the code.

I am hoping someone could taker a peek and show me where I went wrong.

Here's what I got:

<tr>
<td>IP Address:</td>
<td>
<form name="search" method="post" action="<?=$PHP_SELF?>">
<input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form> </td>
</tr>
</TABLE>
</BODY>
</HTML>

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

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

// search for term
$s = long2ip($sip);
$e = long2ip($eip);
$sql = "SELECT * FROM ips WHERE $sip > $find AND $eip < $find";
$data = mysql_query($sql) or die(mysql_error());

// display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['ip'];
echo "<br>";
}

// counts the number or results
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

// string searched for
echo "<b>Searched For:</b> " .long2ip($find);
}
echo'
<br>
<tr>
<td class="selcol1">NIC</td>
<td class="selcol2">'.$nic.'</td>
</tr>';
?>

dreamcatcher

6:28 pm on Aug 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi BadGoat,

How are these two variables defined?

$sip
$eip

I think thats your problem. Try setting your error_reporting level to E_ALL for debugging.

dc

BadGoat

6:42 pm on Aug 29, 2006 (gmt 0)

10+ Year Member



$sip is the starting IP of the range, and $eip is the ending IP in the range. When I add them to the db I enter the IP address into the field and use

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

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

$start is the input name for the starting IP address and $end is the input name for the ending IP address on the form to add the IP range.

I've gotten the error to go away with some proper punctuation, the problem now is that when I enter in an IP address I know is within a range in my db, I get the error message that it cannot find a match... so I am wondering if the problem lies in the translation from long to IP or vice-versa as it compares the IP I enter and the long form stored in the database?

[1][edited by: BadGoat at 6:46 pm (utc) on Aug. 29, 2006]