Getting country from IP address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runway27
    Banned
    New Member
    • Sep 2007
    • 54

    Getting country from IP address

    i am using $ip= $_SERVER['REMOTE_ADDR'] to retrieve the ip address of the client for example if the value returned from $ip

    is 50.160.190.150

    i would like to find out which country the request has come from. i believe by using the third set of numbers (in this case=190) from an ip address we can find out the country name. i can declare a variable with a list of country names and the range of values, what i need help is to extract the 3rd set of numbers from $ip.

    as ip address keep changing the 3rd set of numbers can be single, double or 3 digit number how can we extract the number that is stored in $ip after the second dot and before the third dot

    please advice.

    thanks.
  • realin
    Contributor
    • Feb 2007
    • 254

    #2
    hey this lil piece of code may help you, if i understood your problem correctly.

    [PHP]<?php

    $ip="50.160.190 .150";
    $str=explode(". ",$ip);
    echo "This will always give you the number that is stored in $ip after the second dot and before the third dot ".$str[2];
    ?>[/PHP]

    cheers !!

    Comment

    • nitinpatel1117
      New Member
      • Jun 2007
      • 111

      #3
      The third part of an IP addresses doesn't give you the country of the user.

      You have to use the whole IP address, i.e. all four numbers.
      against a database of IP ranges that are allocated to certain Internet services providers (ISP). ISPs in them selves are only allocated a set of IP ranges in a country or region. Or something along those lines.

      see http://www.iana.org/numbers/

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Runway.

        You can use PHP's GeoIP extension to do this (http://php.net/geoip).

        Comment

        • chrislim2888
          Banned
          New Member
          • Mar 2012
          • 1

          #5
          You need the entire IP address to get the country, city or region information.

          Check this http://www.fraudlabs.com for free license, and http://www.ip2location.com for paid license

          Comment

          Working...