SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting visitors country from their IP

    hi,...
    i want to get visitors country via their IP..
    right now I'm using this (http://api.hostip.info/country.php?ip=)
    here's my code

    PHP Code:
    $ip_arg_1=$_SERVER['HTTP_CLIENT_IP'];
    $ip_arg_2=$_SERVER['HTTP_X_FORWARDED_FOR'];
    $ip_arg_3=$_SERVER['REMOTE_ADDR'];
    if (isset(
    $_SERVER['HTTP_CLIENT_IP'])) {$real_ip_adress=$_SERVER['HTTP_CLIENT_IP'];} 
    else if (isset(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {$real_ip_adress=$_SERVER['HTTP_X_FORWARDED_FOR'];} 
    else {
    $real_ip_adress=$_SERVER['REMOTE_ADDR'];}
    $cip=$real_ip_adress;
    $iptolocation='http://api.hostip.info/country.php?ip='.$cip;
    $creatorlocation=file_get_contents($iptolocation); 
    well, it's working properly..
    but, the thing is: the country i get is actually country code like US or CA..
    and i want the whole country name like United States or Canada
    • so, is there any good alternative to hostip.info offers this?



    i know that i can just write some code that will eventually turn this two letters to whole country name
    but i'm just too lazy to write a code that contains all countries...

    P.S: for some reason i don't want to use any ready made CSV file or any code that will grab this information for me.. like ip2country ready made code and CSV

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have a look at the following service, it was only a quick search but it looks very promising.

    http://ipinfodb.com/ip_location_api_json.php

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,054
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alison3011 View Post
    hi,...
    i want to get visitors country via their IP..
    right now I'm using this (http://api.hostip.info/country.php?ip=)
    here's my code

    PHP Code:
    $ip_arg_1=$_SERVER['HTTP_CLIENT_IP'];
    $ip_arg_2=$_SERVER['HTTP_X_FORWARDED_FOR'];
    $ip_arg_3=$_SERVER['REMOTE_ADDR'];
    if (isset(
    $_SERVER['HTTP_CLIENT_IP'])) {$real_ip_adress=$_SERVER['HTTP_CLIENT_IP'];} 
    else if (isset(
    $_SERVER['HTTP_X_FORWARDED_FOR'])) {$real_ip_adress=$_SERVER['HTTP_X_FORWARDED_FOR'];} 
    else {
    $real_ip_adress=$_SERVER['REMOTE_ADDR'];}
    $cip=$real_ip_adress;
    $iptolocation='http://api.hostip.info/country.php?ip='.$cip;
    $creatorlocation=file_get_contents($iptolocation); 
    well, it's working properly..
    but, the thing is: the country i get is actually country code like US or CA..
    and i want the whole country name like United States or Canada

    • so, is there any good alternative to hostip.info offers this?



    i know that i can just write some code that will eventually turn this two letters to whole country name
    but i'm just too lazy to write a code that contains all countries...

    P.S: for some reason i don't want to use any ready made CSV file or any code that will grab this information for me.. like ip2country ready made code and CSV
    If you use their http://api.hostip.info/get_json.php url, you can get the country_name. You will need to run it through json_decode() so it builds an object you can utilize in PHP, but that shouldn't be too difficult.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

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