PHP installation of GeoIP extension based on the IP address to obtain the geographic location and calculation distance method, geoip geographic location

Source: Internet
Author: User
Tags geoip maxmind

PHP installation of GeoIP extension based on the IP address to obtain the geographic location and calculation distance method, geoip geographic location

Obtain the visitor's country, city, and latitude and longitude Based on the IP address
Install GeoIP extension:

sudo apt-get install libgeoip-dev
pecl install geoip-1.1.0 

Note: The Beta version should be specified. If it is apt installed PHP, directly install the php5-geoip package.
Add the following to php. ini:

extension=geoip.sogeoip.custom_directory="/usr/share/GeoIP"

Download the GeoLiteCity database for free (18 MB after decompression ):
Http://dev.maxmind.com/geoip/legacy/install/city/

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzgunzip GeoLiteCity.dat.gzsudo mkdir -v /usr/share/GeoIPsudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

Test:

php -a
<? Phpprint_r (geoip_record_by_name ('2017. 37.165.80 ')); // press Ctrl + D to run Array ([continent_code] => AS [country_code] => CN [country_code3] => CHN [country_name] => China // country [region] => 22 [city] => Beijing // city [postal_code] => [latitude] => 39.928901672363 // latitude [longpolling] => 116.38829803467 // longitude [dma_code] => 0 [area_code] => 0)

Use geoiplookup on the command line to view IP information:

traceroute www.oschina.net 

Visible IP Address

 61.145.122.155
sudo apt-get install geoip-bin geoip-databasegeoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIP.datGeoIP Country Edition: CN, China

Geoip-The GeoIP. dat provided by geoip-database can only be accurate to countries.

geoiplookup 61.145.122.155 -f /usr/share/GeoIP/GeoIPCity.datGeoIP City Edition, Rev 1: CN, 30, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0, 0

From the database GeoLiteCity under the maxmind official website, the information is more detailed.

Geoiplookup 61.145.122.155 displays the information of the above two databases.

Determine longitude and latitude and computing distance based on IP Address

Available

geoip_record_by_name($_SERVER['REMOTE_ADDR'])

Determine the longitude and latitude based on the user IP address.
Note:

geoip_record_by_name()

The return longitude and latitude are negative.

5000 meters to latitude and longitude:
Latitude: 1 deg = 110852 m
Longitude longpolling: 1 deg = 111320 * cos (lat) m
The same longitude line has a latitude difference of about 110852 meters.
On the same latitude line, the difference is about 111320 X cos (lat) meters (lat is the latitude of the weft)

<? Php // centered on the current user's latitude and longitude, query other users within 5000 m $ y = 5000/110852; // The latitude range is $ x = 5000/(111320 * cos ($ lat )); // longitude range $ SQL = 'select * from user where lat >=( $ lat-$ y) and lat <= ($ lat + $ y) and lon> = ($ lon-$ x) and lon <= ($ lon + $ x );';

In the Database User table, two fields are set to store the user's longitude lat and latitude lon.

($lat-$y) <= lat <= ($lat+$y)($lon-$x) <= lon <= ($lon+$x)

This range is a rough range. After calculating the distance below, you can remove users more than 5 kilometers.

Based on the longitude and latitude of the user found above,
Use the semi-normal vector formula (Haversine) to calculate the distance between two points based on the latitude and longitude:

<? Phpfunction distance ($ lat1, $ lon1, $ lat2, $ lon2) {$ R = 6371393; // average Earth radius, unit: meters $ dlat = deg 2rad ($ lat2-$ lat1); $ dlon = deg 2rad ($ lon2-$ lon1 ); $ a = pow (sin ($ dlat/2), 2) + cos (deg 2rad ($ lat1) * cos (deg 2rad ($ lat2 )) * pow (sin ($ dlon/2), 2); $ c = 2 * atan2 (sqrt ($ a), sqrt (1-$ )); $ d = $ R * $ c; return round ($ d);} echo distance (0, 0,-1, 0); // 111202 meters

Then, you can use uasort or array_multisort to list users from near to far. For example, the users named win, osx, and lin are listed as follows:

<? Php $ arr = array ('win' => array ('dis '=> 1024, 'age' => 31 ), 'osx' => array ('dis '=> 512, 'age' => 15), 'lin' => array ('dis' => 512, 'age' => 25); foreach ($ arr as $ k => $ v) {$ sort ['dis '] [$ k] = $ v ['dis']; $ sort ['age'] [$ k] = $ v ['age'];} // sort by distance in ascending order. If the distance is the same, array_multisort ($ sort ['dis '], SORT_ASC, $ sort ['age'], SORT_DESC, $ arr); echo json_encode ($ arr ); // {"lin": {"dis": 512, "age": 25}, "osx": {"dis": 512, "age": 15 }, "win": {"dis": 1024, "age": 31 }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.