SQL format. Convert IP address to IP number
The table worldip contains fields start, end and code.
Every row means a network. start and end are integer fields with IP numbers, code is a double-character country code, like "US".
You can convert every IP address to IP number with following formula:
For example, IP Address 91.121.147.207 converts to the IP number 1534694351
Than you can get the country code with following request:
If you use API (to get the most current date), you can install only one tabel with country names.
You can convert every IP address to IP number with following formula:
IP Number = 16777216*A + 65536*B + 256*C + D
where IP Address = A.B.C.D
There is function ip2long($ip_address) in PHP that returns IP number. For example, IP Address 91.121.147.207 converts to the IP number 1534694351
Than you can get the country code with following request:
SELECT code FROM worldip WHERE start<=IP_NUMBER and end>=IP_NUMBER
If you install second table with country names, you get both country code and name with this request:
SELECT wl.code,wl.country FROM worldip w LEFT JOIN worldip_land wl ON (w.code=wl.code) WHERE w.start<=IP_NUMBER and w.end>=IP_NUMBER
or just country name:
SELECT wl.country FROM worldip_land wl LEFT JOIN worldip w ON (w.code=wl.code) WHERE w.start<=IP_NUMBER and w.end>=IP_NUMBER
If you use API (to get the most current date), you can install only one tabel with country names.
SELECT country FROM worldip_land WHERE code="GB"gives back "United Kingdom"Post comment
EnglishDeutschРусский
mgyk
02 Sep 2008 13:09#12Русский
02 Sep 2008 13:09#12Русский
Дима
02 Sep 2008 19:09#13Русский
02 Sep 2008 19:09#13Русский
Alex 02 Sep 2008 20:09#14Русский
Ncs
03 Sep 2008 12:09#15Русский
03 Sep 2008 12:09#15Русский
