Block user registration by countries 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 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'"); // 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 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

+4
source share
4 answers

SQL- :

SELECT `country_iso2` FROM `countryip` WHERE `ip_from` <= inet_aton('$ip') AND ip_to >= inet_aton('$ip')

ISO- IP-. ISO .

+3

, IP - .

MaxMind GeoLite2, :

https://github.com/maxmind/GeoIP2-php

+1

PHP Extension API, . 7 PHP (C API). MySQL , IP- SQL . Maxmind , .

, mmdb PHP 2- iso-3316:

Maxmind GeoLite2 Kohana PHP

Geolocate IP Address

0

All Articles