Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    137
    Thanks
    1
    Thanked 0 Times in 0 Posts

    using string functions

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Using explode will allow you to split it into pieces:
    PHP Code:
    $ip '50.160.190.150';
    $aIP explode('.'$ip);
    /* IP Now contains:
    $aIP[0] = 50;
    $aIP[1] = 160;
    $aIP[2] = 190;
    $aIP[3] = 150;
    */ 
    You'll be in for a surprise if you think that the third octlet is relational to the country though...
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Its usually the first one.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •