Access the country/city/latitude and longitude of the visitor based on IP
Install GeoIP Extensions:
sudo apt-get install Libgeoip-dev
Note: The beta version is to specify the version number. If it is apt to install PHP, directly install PHP5-GEOIP this package can.
PHP.ini Add:
extension=geoip.so
geoip.custom_directory= "/usr/share/geoip"
Free download of geolitecity database (18MB after decompression):
http://dev.maxmind.com/geoip/legacy/install/city/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir-v/usr/share/geoip
sudo mv-v geolitecity.dat/usr/share/geoip/geoipcity.dat
Test:
<?php
Print_r (geoip_record_by_name (' 106.37.165.80 '));//Enter and press Ctrl+d to run
Array
(
[Continent_code ] => as
[Country_code] => CN
[country_code3] => CHN
[country_name] => China//Country
[Region] =& Gt [
City] => Beijing//Cities
[Postal_Code] =>
[latitude] => 39.928901672363//Latitude
[longitude] => 116.38829803467//Longitude
[Dma_code] => 0
[Area_code] => 0
)
To view IP information with geoiplookup at the command line:
Visible IP Address
sudo apt-get install geoip-bin geoip-database
geoiplookup
61.145.122.155-f GeoIP Country EDITION:CN,
The GeoIP.dat provided by Geoip-database can only be accurate to the country.
Geoiplookup 61.145.122.155-f/usr/share/geoip/geoipcity.dat
GeoIP City Edition, Rev 1:cn, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0, 0
From the Maxmind official website of the database geolitecity information more detailed.
Geoiplookup 61.145.122.155 also displays information about the two databases listed above.
Determining latitude and longitude based on IP and calculating distance
can use
Geoip_record_by_name ($_server[' remote_addr ')
Determine latitude and longitude according to user IP.
Attention:
The return of the longitude and latitude is negative.
5000 meters turn to warp latitude:
Latitude latitude:1 deg = 110852 m
Longitude longitude:1 deg = 111320*cos (LAT) m
On the same meridian, the difference of one latitude is about 110852 meters.
On the same weft, the difference is about 111320*cos (LAT) m (lat is the latitude of the weft)
<?php//
to the current user latitude and longitude of the center, query 5000 meters of other users
$y = 5000/110852;//latitude range
$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);
';
The database user table is set up with two fields that store the longitude lat and latitude lon of the user respectively.
($lat-$y) <= lat <= ($lat + $y)
($lon-$x) <= Lon <= ($lon + $x)
This range is a rough range, the following calculated distance after more than 5 kilometers of users can be removed.
According to the above query of the user's latitude and longitude,
Using the half positive vector formula (Haversine) to calculate the distance between two points according to the latitude and longitude:
<?php
function Distance ($lat 1, $lon 1, $lat 2, $lon 2) {
$R = 6371393;//Earth mean radius, unit m
$dlat = Deg2rad ($lat 2-$ LAT1);
$dlon = Deg2rad ($lon 2-$lon 1);
$a = Pow (sin ($dlat/2), 2) + cos (Deg2rad ($lat 1)) * cos (Deg2rad ($lat 2)) * POW (sin ($dlon/2), 2);
$c = 2 * atan2 (sqrt ($a), sqrt (1-$a));
$d = $R * $c;
Return round ($d);
echo distance (0, 0,-1, 0); 111202 meters
You can then use Uasort or Array_multisort to list users from near to far, such as the 3 users known as Win,osx,lin:
<?php
$arr = Array ('
win ' => array (
' dis ' => 1024,
' age ' =>
),
' OS X ' => array (
' dis ' =>,
' => '
,
' Lin ' => array (
' dis ' =>,
' age ' =>
)
);
foreach ($arr as $k => $v) {
$sort [' dis '] [$k] = $v [' dis '];
$sort [' Age '] [$k] = $v [' age '];
}
Sorted by distance in ascending order, if the distance is the same, sort
array_multisort by age ($sort [' dis '], SORT_ASC, $sort [' age '], Sort_desc, $arr);
echo Json_encode ($arr);
{"Lin": {"dis": "31", "Age":}, "OS X": {"dis": "," "Age":}, "Win": {"dis": 1024, "Age":}}