Home >Backend Development >PHP Tutorial >http - Is there a way in php to get the geographical location based on the ip address without using a third-party interface? (How do third-party interfaces implement this function?)
I am very curious about the principles of tools that obtain geographical location based on IP? What does each number in IP represent? Can I accomplish this locally without using a third-party interface?
I am very curious about the principles of tools that obtain geographical location based on IP? What does each number in IP represent? Can I accomplish this locally without using a third-party interface?
What is an ISP? To put it simply, it is like network providers such as China Telecom, China Unicom, and China Mobile.
Once you have a supplier, you can go online. When you go online, you will be able to obtain a temporary IP (cyclical change) through MODEM.
But is this IP really given randomly? Why is it so random?
APNIC (Asia-Pacific Internet Network Information Center) is this organization. It is one of five regional Internet registries now operating in the world, an international organization that assigns Class B IP addresses. It provides global distribution and registration services that support Internet operations. These members include Internet service providers, the National Internet Registry, and similar organizations in a non-profit, membership-based organization. APNIC is responsible for the Asia-Pacific region, which includes 56 economic zones.
Suppose that APNIC allocates 1 million IP addresses to China. Then it goes to China and is distributed to top ISPs such as China Telecom, China Mobile, China Unicom, and China Mobile through the top domestic management organization (I don’t know who it is). Then these ISPs will redistribute them to different provinces across the country according to their own rules, and then to cities, counties, towns, etc.
Each number in the IP address does not mean anything. The IP allocation of the top domestic ISPs is public. Therefore, only by getting this library can we know where the user is based on the user's IP. Since the update of this library is not timely, So it’s not 100% accurate
The more popular IP library in China is Innocence. Official website: http://www.cz88.net/ You can download it in the upper right corner. After installation, there is a .dat
file in the directory, which is the IP allocation database. Combining the use of PHP with the use of other languages can be found on Baidu.
PHP installs the GeoIP extension and database to obtain the visitor's country/city/latitude and longitude based on the IP.
http://php.net/manual/zh/ref....sudo apt-get install libgeoip-dev
pecl install geoip-1.1.0
Note: Beta version needs to specify the version number. If it is PHP installed by apt, just install the php5-geoip package directly. Add:
to php.ini
<code>extension=geoip.so geoip.custom_directory="/usr/share/GeoIP" </code>Free download of GeoLiteCity database (18MB after decompression):
http://dev.maxmind.com/geoip/...
wget http://geolite.maxmind.com/do...
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
php -a
<code><?php print_r(geoip_record_by_name('106.37.165.80')); 回车后按Ctrl+D运行 Array ( [continent_code] => AS [country_code] => CN [country_code3] => CHN [country_name] => China //国家 [region] => 22 [city] => Beijing //城市 [postal_code] => [latitude] => 39.928901672363 //纬度 [longitude] => 116.38829803467 //经度 [dma_code] => 0 [area_code] => 0 ) </code>PHP easily implements the "people nearby" function, determines longitude and latitude based on IP, and calculates distance based on longitude and latitude
Do not use third-party interface, you also need to use the IP library provided by the third party for matching query.
But it is not 100% accurate.
They all have their own IP library. Unless you also have an IP library locally
The third-party interface can obtain geographical location information through
ip. In fact, it locally saves the database mapping of
ip => address -info, because their database stores a large number of relationships between
ip and geographical location. , so we can provide us with third-party services
If you still don’t want to request a third-party interface, you can download one
Free GeoIP database
This is a database problem. There is an open source code here. Go and study it yourself. http://git.oschina.net/lionsoul/ip2region
nginx+geo+fastcgi_params
Welcome phper to join hnphper group 233415606
pecl install qqwry-beta
qqwry You deserve it.
IP is actually not divided randomly. If you are careful, you will find it. The IP segment and the region are closely linked. The IPs of your next city and your city may be consecutive.