i use this function to get client IP it work
function get_client_ip() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$ip = get_client_ip();
but how i can get latitude and longitude by sever side and not client side ?
PHP does not have the in-built support for that. You could make use of third party libraries like
ip2location
to grab the longitude and latitude from the ip address.Sidenote : Determining the latitude – longitude through an IP Address may not fetch you accurate information.
Uses geoplugin.net. See if this suits you.
OUTPUT :
Best option from php.net which is also open source: Geo IP
You can install it using following steps. Which are given on the link I have provided:
Run these commands in terminal.
Hope it helps!!
You should consider getting the exact location of the user from client side using Javascript API
navigator.geolocation
. The advantage of using this is, it will be accurate.The issue with IP to Geo is that, there is no guarantee that the info will be accurate. It depends on how updated the data is.
On the other hand, the javascript API directly takes the geo based on GPS or Network address (whichever is accurate). But the user will have to give permission to access to his GPS.
You can use the Javascript (if user gives permission) or fall back to ip to geo conversion
How to Use
For a quick, copy-and-paste solution, I have written code for accomplishing this task.
I use this function:
Outputs:
It uses the (free) freegeoip.net API which I have found to be extremely fast with a comprehensive database. Being free, it has a limit of 10000 requests per hour (just under 3 per second), which should suffice for most projects’ needs.