Block user registration by country ip range
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 have added the entire ip ranges of all countries in 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 echo a message. From what i
undestood i need to start by declaring
<?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'");
// this works only if i have the single static ip in the dbs, but now
i have the ip range
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 query
$query = mysql_query("SELECT `column` FROM `table` WHERE `column` = '$ip'");
in order to make possible the comparison between the User IP and the ip
range.
And the IP FROM column, TO IP column looks like this 16777216, 16777471,
In order to find out the ip there was a formula similar with ip = 256 +
CLASS B * 256 + CLASS C * 256 * 256 + CLASS D * 256 * 256 * 256
No comments:
Post a Comment