Jump to content


Photo

IP Validation and ip2long, long2ip


  • Please log in to reply
27 replies to this topic

#1 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 02:01 PM

Hello!

I've written a script which is supposed to search my MySQL db for a matching IP address. When I search for a matching IP I am always getting the error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" which refers to my query, which is:
$data = mysql_query("SELECT * FROM ips WHERE find = ".long2ip($sip).""); 

As you can see, I am using ip2long to convet the IP address string  to the proper address and long2ip to convert it back to the IP address string. I'm sure I can't see the forest for the trees, as I've been banging my head for the whole weekend. Can someone more experienced see the error in my script?

<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
$data = mysql_query("SELECT * FROM ips WHERE find > ".long2ip($sip)." AND < ".long2ip($eip)."");

// 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, no entry matched your query<br><br>";
}

// string searched for
echo "<b>Searched For:</b> " .long2ip($find);
}
?>


#2 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 28 August 2006 - 02:04 PM

Maybe I am being dumb,...but you could try this

$data = mysql_query("SELECT * FROM `ips` WHERE `find` = '.long2ip($sip).'");
 

instead of this:

$data = mysql_query("SELECT * FROM ips WHERE find = ".long2ip($sip)."");
 

#3 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 02:20 PM

Thanks for the quick reply! But it didn't work, same MySQL error. :(

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 02:31 PM

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 August 2006 - 02:34 PM

That message usually comes up when you you don't use the proper format or syntax. As crayon suggested, his would be the proper way to find data between variables.

Ray

#6 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 02:48 PM

Hello Crayon,

Thank you for that! I am closer now. Now I get a new error: Unknown column 'find' in 'where clause', so I changed 'find' to '$find' and I search for an IP I know is in the db, but it says that it can find no matches. Am I doing something wrong in this part of the code which prevents me from finding a known IP?
// 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]);


#7 AndyB

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

Posted 28 August 2006 - 02:51 PM

The error message means that you don't have a database field named find. Change it in the query so that you are testing for the correct field name.

$sql = "SELECT * FROM ips WHERE name_of_field_containing_ips BETWEEN '$s' AND '$e'";

Legend has it that reading the manual never killed anyone.
My site

#8 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 02:57 PM

Andy, in your example, 'name_of_field_containing_ips' is the IP I am entering into the search field, and '$s' AND '$e' reference the starting IP and the ending IP of fields in my database.  I am guessing I am constructing it incorrectly from the start?

#9 AndyB

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

Posted 28 August 2006 - 03:00 PM

No it isn't, name_of_field_containing_ips is the name of the field in your database not the name of some program variable.  Since the query error tells you the database table fieldname isn't named 'find', it must be named something else.
Legend has it that reading the manual never killed anyone.
My site

#10 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 03:05 PM

My pardons, I wanted to say that what I was hoping to do was to search for the IP address I entered in my search field and match it as being between my two fields in my database, $sip and $eip, which Crayon helped convert correctly with long2ip to $s and $e. With what I am trying to do in mind, how do I match the entered IP address to a range of IP addresses between $s and $e?

#11 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 03:06 PM

so...what exactly is $s and $e then ($sip and $eip)?

basically your query says this:

select everything from your table called ips where one of your fields (your ip address field, presumably) is between $s and $e. 

this will potentially give you more than one answer.  are you expecting to get the rest of the information (like username, etc..) based on the ip address, and expect one result?

i think you are going to have to be more specific on what you are trying to accomplish here. what are you expecting the user to enter in the field, and what are you expecting the query to do with it?

edit (cuz you posted when i was writing) okay if $s and $e are ip ranges you have set somewhere else, presumably all you need to do is change the "find" in the query to the name of the column that holds your ip addresses
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#12 AndyB

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

Posted 28 August 2006 - 03:11 PM

.. or select everything from the database table where what the user entered is less than one database field and more than another database field ...
Legend has it that reading the manual never killed anyone.
My site

#13 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 August 2006 - 03:16 PM

I'm a noob :)  I'll try to explain what I am trying to accomplish.

So I have a db with IP ranges. $sip is the starting IP of the range. $eip is the ending IP of the range. I am trying to create a searcxh script which will allow me to type in an IP address and when it finds a matching range, it will echo back some cursory information about the IP range. For example, 'Bob' is the 'IP Admin' for the IP Range between 10.10.10.0 ($sip) and 10.10.11.255($eip) and 'Mary' is the 'Tech Support' for the IP Range between 10.0.0.0($sip) and 10.0.255.255($eip).

My hopes was to be able to type in an IP address into the search field and whem it falls into the IP range, the script gives me the info I am seeking.

#14 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 12:28 PM

.. or select everything from the database table where what the user entered is less than one database field and more than another database field ...

Aye, that's what I aim to do, but I am unsure how to do it. Can you point me in the right direction or cite an example please?

#15 AndyB

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

Posted 29 August 2006 - 12:52 PM

Approximately ...

SELECT * from your_database_table WHERE sip > '$user_input' AND eip < '$user_input'
Legend has it that reading the manual never killed anyone.
My site

#16 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 01:00 PM

Hi Andy,

I tried that on my own yesterday but I thought it was wrong since it gave me an odd error, which is "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 '> '184615169' AND < '184615169'' at line 1"

#17 AndyB

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

Posted 29 August 2006 - 01:36 PM

Notice the slight difference between what I suggested and what you tried - there's a variable in EACH test in mine.  That style of query should work for you.
Legend has it that reading the manual never killed anyone.
My site

#18 BadGoat

BadGoat
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 29 August 2006 - 01:40 PM

The error reported above was given with this MySQL query:

$sql = "SELECT * FROM ips WHERE $sip > $find AND $eip < $find";

#19 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 29 August 2006 - 05:19 PM

after your $sql = " .... ";

echo out $sql and see what it's actually producing (and post it here)


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#20 AndyB

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

Posted 29 August 2006 - 05:30 PM

Punctuation is very important.  The code I've suggested doesn't seem to be one you've tried exactly yet.

$query = "SELECT * FROM ips WHERE sip > '$find' AND eip < '$find'"; // exactly like this
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // explain what's wrong

Give that a shot and post the precise output you get.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users