Time ago I used to access to Maxmind local copy of GeoIP database with some simple functions in php.
Basically their database was with a schema like:
CREATE TABLE `geoip_city_blocks` (
`startIpNum` INT(10) UNSIGNED NOT NULL,
`endIpNum` INT(10) UNSIGNED NOT NULL,
`locId` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`startIpNum`, `endIpNum`),
INDEX `startIpNum` (`startIpNum`),
INDEX `endIpNum` (`endIpNum`),
INDEX `locId` (`locId`)
)
In order to get country/city informations for a certain IP you had just to convert it to it's numeric counterpart with a:
$numeric_ip = ip2num($ip);
where ip2num() is:
function ip2num($ip) {
$ip = explode(".",$ip);
return (( (int) $ip[0] ) * 16777216) + (( (int) $ip[1] ) * 65536) + (( (int) $ip[2] ) * 256) + (( (int) $ip[3] ) * 1);
}
And then cast a simple query:
SELECT * FROM geoip_city_blocks AS blocks LEFT JOIN geoip_city_locations AS locations ON (blocks.locId = locations.locId) WHERE ".$numeric_ip." >= startIpNum AND ".$numeric_ip." <= endIpNum LIMIT 1
This was good because with any database MySQL, SQLite, Postgre.. and so on you can cas that query and just compare 2 integers..
With the new version of GeoIP you have this new kind of schema:
CREATE TABLE blocks(
"network" TEXT,
"geoname_id" TEXT,
"registered_country_geoname_id" TEXT,
"represented_country_geoname_id" TEXT,
"is_anonymous_proxy" TEXT,
"is_satellite_provider" TEXT
);
Where network is espressed in a way like 120.120.120.120/8 like CIDR addresses.. and there's no StartIpNum
and EndIpNum
You can see it in the image:
How can I cast a query now that I can't search via StartIPNum
and EndIpNum
?