Location of IP addresses

Discussion in 'Programming/Scripts' started by roadrunner, Apr 28, 2005.

  1. roadrunner

    roadrunner New Member

    Hi,

    is it possible to look up the location of IP addresses in PHP? I'd like to allocate a country to an IP address.
     
  2. falko

    falko Super Moderator Howtoforge Staff

  3. roadrunner

    roadrunner New Member

    Thanks! :)
     
  4. ifour.parth

    ifour.parth New Member

    <?php
    function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
    $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
    $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
    $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
    else
    $ipaddress = 'UNKNOWN';
    return $ipaddress;
    }

    echo $get_client_ip;
    ?>

    <?php
    $PublicIP = get_client_ip();
    $json = file_get_contents('https://api.snoopi.io/$PublicIP?apikey=[YOUR API KEY]');
    $json = json_decode($json ,true);
    ?>
     
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    The question was answered correctly a very long time ago. Your shared code did not anwer that question in any way though.
     

Share This Page