📜 ⬆️ ⬇️

IP Geography

Recently, I had to deal with a program for keeping statistics on clicks of a certain system of ad exchange. When you clicked, as usual, information about the click was collected (on which page, what time, browser, ID of the advertisement and IP occurred). In the presentation of statistics was the task - to find out the city from which the transition occurred. Previously, I was not particularly interested in this; therefore, only now I have begun a deeper acquaintance with GeoIP systems.

Also, how was it possible

First, I remembered all sorts of geoip tables there, which can now be found without much difficulty more difficult than before. But abbreviated options lie on the sites-collectors of bases. Below is one of them.
')
reduced base
maxmind.com is the site itself. there you can check the geoip base.

The variant in my case was dropped - the whole list would have to, but it is not small and it is necessary to make selections from the tables. and after some time it will be necessary to update the table. In general, laziness made me look for other ways. While searching, I found an interesting service http://geoiptool.com/ which performs the standard operation of any geoip service, but also imposes on a google map .



Very funny. Habr - here

As a result, I found http://www.ip2city.ru/ - an open base of IP addresses. I stopped at it. What distinguished it among other services is the ability to provide data of the form
field1 = value1
field2 = value2
...
what is called by a simple get request www.ip2city.ru/ip2city.php?ip=xxx.xxx.xxx.xxx

and such data is fairly easily received from third-party servers and parted

$ IP = "00.00.00.00"; // or ah-pi from anywhere (in my case from the database)

$ lines = file ('http://www.ip2city.ru/ip2city.php?ip='.$$);

$ city = trim (str_replace ('=', '', strstr ($ lines ['0'], '=')));

$ country = trim (str_replace ('=', '', strstr ($ lines ['4'], '=')));

Naturally, with such work, PHP will take a long time to draw a page until it receives all the data from the server (the parsing itself is fast), so I would recommend the IP -> city operation to be performed after the page loads.

This is my first topic, I apologize in advance for errors. I hope someone will come in handy

Source: https://habr.com/ru/post/25486/


All Articles