The following is a modified version of a snippet I found that uses http://ipinfodb.com/ip_locator.php to get its information. Keep in mind, you can also apply for an API key with them and use the API directly to get the information supplied as you see fit.
detect_location();
// returns "CITY, STATE" based on user IP
detect_location('xxx.xxx.xxx.xxx');
// returns "CITY, STATE" based on IP you provide
detect_location(NULL, TRUE); // based on user IP
// returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }
detect_location('xxx.xxx.xxx.xxx', TRUE); // based on IP you provide
// returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }
Home » PHP » Get Geolocation (Country, Latitude, and Longitude) from IP Address using PHP
Geolocation provides information about the geographic location of a user. Specifically, the IP address is used by the geolocation service to determine the location. To track the visitor’s location, the first thing needed is an IP address. Based on the IP address, we can collect the geolocation info
of the visitor. The PHP $_SERVER variable is the easiest way to get the user’s IP address. Based on the visitor’s IP address, you can detect the location with latitude and longitude using PHP. In this tutorial, we will show you how to get location from IP address using PHP.
The Geolocation API is an instant way to find the location of a user by IP address. You can use a free Geolocation API in PHP to fetch location information from an IP address. This example script
will use IP Geolocation API to get location, country, region, city, latitude, and longitude from IP address using PHP.
Get IP Address of User with PHP
Use the REMOTE_ADDR index of $_SERVER to get the current user’s IP address in PHP.
$userIP = $_SERVER['REMOTE_ADDR'];
Get Location from IP Address using PHP
Use the IP Geolocation API to get the user’s location from IP using PHP.
Call API via HTTP GET request using cURL in PHP.
Convert API JSON response to array using
json_decode() function.
Retrieve IP data from API response.
There are various info is available about geolocation in API response. Some of the most useful location details are:
Country Name (country_name)
Country Code (country_code)
Region Code (region_code)
Region Name (region_name)
City (city)
Zip Code (zip_code)
Latitude (latitude)
Longitude (longitude)
Time Zone (time_zone)
<?php // IP address $userIP = '162.222.198.75'; // API end URL $apiURL = 'https://freegeoip.app/json/'.$userIP; // Create a new cURL resource with URL $ch = curl_init($apiURL); // Return response instead of outputting curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute API request $apiResponse = curl_exec($ch); // Close cURL resource curl_close($ch); // Retrieve IP data from API response $ipData = json_decode($apiResponse, true);
Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request
If you have any questions about this script, submit it to our QA
community - Ask Question
How can I get current location from IP address in PHP?
From PHP.net: The GeoIP extension allows you to find the location of an IP address. City, State, Country, Longitude, Latitude, and other information as all, such as ISP and connection type can be obtained with the help of GeoIP. This is just a php module for the MaxMind database, which is payware.
How do you get location from IP address?
There is no method of associating an exact physical geographical address or the computer associated with an IP address available to an end-user. If you need to report abuse by a person behind an IP address, contact local authorities or the ISP who's in control of that IP address.
How get lat long from IP address in PHP?
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.
Where is geolocation of an IP address?
IP geolocation is the mapping of an IP address to the geographic location of the internet from the connected device. By geographically mapping the IP address, it provides you with location information such as the country, state, city, zip code, latitude/longitude, ISP, area code, and other information.