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 :

https://ipinfodb.com/ip_location_api.php

 


https://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=117.10.168.1&format=json

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

<?php

echo $_SERVER['REMOTE_ADDR'];

?>

Here is the entire code.


<?php
$url = json_decode(file_get_contents("https://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=".$_SERVER['REMOTE_ADDR']."&format=json"));

//print the array to see the fields if you wish.
//print_r($url);

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

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

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

        1. I think ‘allow_url_fopen’ is set to 0 in your php.ini file. If you have access to php.ini file change ‘allow_url_fopen’ to 1. Then only this script will work..

    1. I think the best solution, as a Website developer, would be to enable allow_url_fopen and just not be careless. Use only trusted networks. But, If you want to be extra safe, then turn off it and use cURL instead.

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

    Snippet

    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 == '127.0.0.1' || $ip == 'localhost') {
    $ip = '8.8.8.8';
    }

    $url = 'https://ipinfodb.com/ip_locator.php?ip=&#039; . urlencode($ip);
    $i = 0; $content; $curl_info;

    while (empty($content) && $i 1,
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_TIMEOUT => 1,
    CURLOPT_REFERER => ‘https://’ . $_SERVER[‘HTTP_HOST’],
    );
    if (isset($_SERVER[‘HTTP_USER_AGENT’])) $curl_opt[CURLOPT_USERAGENT] = $_SERVER[‘HTTP_USER_AGENT’];
    curl_setopt_array($ch, $curl_opt);
    $content = curl_exec($ch);
    if (!is_null($curl_info)) $curl_info = curl_getinfo($ch);
    curl_close($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

    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” }

  3. 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.

Leave a Reply

Theme: Overlay by Kaira
Agurchand