Link to home
Get AccessLog in
Avatar of dev09

asked on

PHP MySQL find IP address (between) stored IP address ranges


I am wondering how to code a PHP MySQL 'SQL' query to find the given IP address between 2 stored IP address ranges.

I am trying this:
$theIp  = '';
$result = mysqli_query($link,"SELECT * FROM `ipAddresses` WHERE `start` <= '$theIp' AND `end` >= '$theIp'") or die(mysql_error());

But it doesn't seem to be working correctly.  Should i store the IP data differently? Not in one field.

Thanks in advance!
Avatar of quizwedge
Flag of United States of America image

Link to home
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of Ryan Chong
one of the methods is to convert the IP Address to "IP Numbers" and then do the respective comparison.

in php you can use ip2long function:


in MySQL, use INET_ATON:
ah, ok, think using INET_ATON is enough here, so no need to use php function ip2long.
This article shows how I've used IP numbers in PHP.  I used the MaxMind database to create a RESTful web service that could be used to identify the client country from its IP address.  Working code examples are included.

You might also want to look into the WordPress plugin called Akismet.  It's an anti-spam tool with IP awareness.