Avatar of dev09
dev09
 asked on

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

Hi,

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  = 'xxx.xxx.xxx.xxx';
$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!
PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
quizwedge

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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:

ip2long
http://php.net/manual/en/function.ip2long.php

in MySQL, use INET_ATON:
https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton
Ryan Chong

ah, ok, think using INET_ATON is enough here, so no need to use php function ip2long.
Ray Paseur

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.
https://www.experts-exchange.com/articles/3437/IP-Address-to-Country-in-PHP.html

You might also want to look into the WordPress plugin called Akismet.  It's an anti-spam tool with IP awareness.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck