Get User current location by IP address – Simple PHP Script!

This script will give you the User’s City, State and Country, Latitude, Langitude etc.,

To get the IP Address is easy in PHP right?, but to get the Country and City we need to use some third party API’s

Here is the one, which is free, go to the below link register and get a api key to :<your_api_key>&ip=

Do you know how to find the current IP address of the User?. If you don’t know, please see the below line




Here is the entire code.

$url = json_decode(file_get_contents("<your_api_key>&ip=".$_SERVER['REMOTE_ADDR']."&format=json"));

//print the array to see the fields if you wish.

echo "<table border='1' width='50%' align='center'><tr><td>COUNTRY:</td><td>";
echo $url->countryName;
echo "</td></tr><tr><td>CITY:</td><td>";
echo $url->cityName;
echo "</td></tr><tr><td>STATE OR REGION:</td><td>";
echo $url->regionName;
echo "</td></tr><tr><td>IP ADDRESS:</td><td>";
echo $url->ipAddress;
echo "</td></tr><tr><td>COUNTRY CODE:</td><td>";
echo $url->countryCode;
echo "</td></tr><tr><td>LATITUTE:</td><td>";
echo $url->latitude;
echo "</td></tr><tr><td>LONGITUDE:</td><td>";
echo $url->longitude;
echo "</td></tr><tr><td>TIMEZONE:</td><td>";
echo $url->timeZone;
echo "</td></tr><tr></table>";


This above script will give you the current location such as city, state, country code, latitude, longitude etc.,

You can also see the demo or download from below:

Demo Download

About the Author


Software Engineer, Blogger etc., Visit my Youtube channel - <a href="">Click Here</a>

17 thoughts on “Get User current location by IP address – Simple PHP Script!

    • Author gravatar

      not working for me please help.. 🙁 it gives blank page without IP and its info.. 🙁

    • Author gravatar

      Very nice post. Its easy to make lat & lang value fetch.

    • Author gravatar

      is this ok to set allow_url_fopen to 1 in security point of view?

    • Author gravatar

      Awesome POst man for finding user location by IP address.

    • Author gravatar

      The following is a modified version of a snippet I found that uses 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.


      function detect_location($ip=NULL, $asArray=FALSE) {
      if (empty($ip)) {
      if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) { $ip = $_SERVER[‘HTTP_CLI

      > Blockquote

      ENT_IP’]; }
      elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) { $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’]; }
      else { $ip = $_SERVER[‘REMOTE_ADDR’]; }
      elseif (!is_string($ip) || strlen($ip) < 1 || $ip == '' || $ip == 'localhost') {
      $ip = '';

      $url = '; . urlencode($ip);
      $i = 0; $content; $curl_info;

      while (empty($content) && $i 1,
      CURLOPT_HEADER => 0,
      CURLOPT_URL => $url,
      CURLOPT_REFERER => ‘http://’ . $_SERVER[‘HTTP_HOST’],
      curl_setopt_array($ch, $curl_opt);
      $content = curl_exec($ch);
      if (!is_null($curl_info)) $curl_info = curl_getinfo($ch);

      $araResp = array();
      if (preg_match(‘{City : ([^<]*)}i’, $content, $regs)) $araResp[‘city’] = trim($regs[1]);
      if (preg_match(‘{State/Province : ([^<]*)}i’, $content, $regs)) $araResp[‘state’] = trim($regs[1]);
      if (preg_match(‘{Country : ([^<]*)}i', $content, $regs)) $araResp['country'] = trim($regs[1]);
      if (preg_match('{Zip or postal code : ([^<]*)}i’, $content, $regs)) $araResp[‘zip’] = trim($regs[1]);
      if (preg_match(‘{Latitude : ([^<]*)}i’, $content, $regs)) $araResp[‘latitude’] = trim($regs[1]);
      if (preg_match(‘{Longitude : ([^<]*)}i’, $content, $regs)) $araResp[‘longitude’] = trim($regs[1]);
      if (preg_match(‘{Timezone : ([^<]*)}i’, $content, $regs)) $araResp[‘timezone’] = trim($regs[1]);
      if (preg_match(‘{Hostname : ([^<]*)}i’, $content, $regs)) $araResp[‘hostname’] = trim($regs[1]);

      $strResp = ($araResp[‘city’] != ” && $araResp[‘state’] != ”) ? ($araResp[‘city’] . ‘, ‘ . $araResp[‘state’]) : ‘UNKNOWN’;

      return $asArray ? $araResp : $strResp;

      To Use

      // returns “CITY, STATE” based on user IP

      // 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”] => “” }

      detect_location(‘’, 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”] => “” }

    • Author gravatar

      How to find current location in php

    • Author gravatar

      I’m just dropping my thanks to this, took a while to find but now I’m all good. I also noticed it does not work on my development server I’m running with APACHE but its dope when I try it on live pages.

    • Author gravatar

      its not giving acurate location

    • Author gravatar

      Thanks Man !!

      it is very useful post
      good work keep it !

    • Author gravatar

      I have tried this code. But fields are coming blank.

Leave a Reply

Your email address will not be published. Required fields are marked *