I have the following table in MYSQL
CREATE TABLE IF NOT EXISTS `countryip` (
`ip_from` bigint(10) unsigned NOT NULL DEFAULT '0',
`ip_to` bigint(10) unsigned NOT NULL DEFAULT '0',
`country_iso2` varchar(2) COLLATE utf8_bin NOT NULL DEFAULT '',
`country_iso3` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',
`country_name` varchar(32) COLLATE utf8_bin NOT NULL,
KEY `ip_from` (`ip_from`,`ip_to`))
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
And here I added all ip ranges of all countries of the world.
I want to make in php a script that will compare the user ip with the ip range from this database and after that an echo message. From what I understood, I need to start with the announcement
<?php
mysql_select_db("database_name") or die(mysql_error());
$ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT `IP` FROM `ban_ip` WHERE `IP` = '$ip'");
if (mysql_num_rows($query) > 0)
{
echo ' <!DOCTYPE html> <html> <head> <script type="text/javascript"> alert("You are not allowed to access this website"); window.location.replace("index.php"); </script> </head> <body> </body> </html>';
}
What can I do instead of this request
$query = mysql_query("SELECT `column` FROM `table` WHERE `column` = '$ip'");
to allow comparison between the user's IP address and ip range.
And the IP FROM, TO IP column looks like this: 16777216, 16777471. In order to find out ip, there was a formula similar to ip = 256 + CLASS B * 256 + CLASS C * 256 * 256 + CLASS D * 256 * 256 * 256
source
share