We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,478 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

find county using ip address

$url=$_POST;
$ip = gethostbyname($url);
$long1 = ip2long($ip)

if ($long1 == -1 || $long1 == FALSE)
{ echo 'Invalid IP, please try again';
}
else {

print $url;
echo $ip ?>

bu this code i get ip address but i need the country name aslo with this ip address. is thers any function to get country?
thanks

13
Contributors
11
Replies
6 Years
Discussion Span
3 Months Ago
Last Updated
12
Views
aarya
Junior Poster
139 posts since Sep 2005
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

There is no function that identifies a country using the IP address. You will need to use a GeoIP (also called GeoLocation) database.

There are several freebie GeoIP databases available.
http://www.maxmind.com/app/geoip_country
http://ip-to-country.webhosting.info/

TopDogger
Junior Poster in Training
87 posts since Aug 2005
Reputation Points: 15
Solved Threads: 5
Skill Endorsements: 0

Use <snipped>to get ip to country and other useful information.

attadaved
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi Aatya,

U can use the following function to get the country name from IP address.

function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url]

//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array

//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);

//assing the city name to the array
$ipDetail['city']=$match[2]; 

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code
return $ipDetail;

}

plz try it let me know what's result?

kuldeep04
Newbie Poster
9 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi Aatya,

U can use the following function to get the country name from IP address.

~~~~~~

plz try it let me know what's result?

Hi kuldeep,

I used your script (with minor modifications - needed only to show the country) and it worked splendid.

thanks pal.

Desolator
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks KULDEEP04 !

I've copied and paste your code, set the Google IP and sure enough while running it, I can display the answer:
City: Mountain View, CA Country UNITED STATES!

That's great, Im going to use your code and leave your reference (URL) in it.
My goal is to record myself from where and when the users hit my pages...

I have a problem..
Maybe you can help me:

To apply this code I must change my pages form ext: .HTML to .PHP:
DO YOU KNOW IF THAT WILL AFFECT the ranking of my pages on search engines?

Gravimotion

gravimotion
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The file extension will not affect a new site, but it will affect an existing site that has already been indexed. The old page URLs in the search engines will generate 404 status codes (Page Not Found) unless you use a 301 redirect to redirect the old pages to the new pages.

If you leave the file names the same and only change the extensions, you should be able to add the following to the .htaccess file to take care of the redirection. This will only work on an Apache web server.

RewriteRule ^(.*)\.html$ /$1.php [R=301,L]

Test it with a few pages to make sure it is working correctly. If you enter an old URL with the .html extension, you should see it change to the new .php page in your browser's address window.

TopDogger
Junior Poster in Training
87 posts since Aug 2005
Reputation Points: 15
Solved Threads: 5
Skill Endorsements: 0

hi dear kuldeep04

i use your function.that was great but i change this function based on my need ihope this function can help php developers

function countryCityFromIP($ip){
    if(ip2long($ip)== -1 || ip2long($ip) === false){
        $stt = false ; 
    }else{

        $ipDetail=array(); 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://api.hostip.info/get_json.php?ip=".$ip);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $dtl = json_decode(curl_exec($ch),true);
        curl_close($ch);
        $stt = array(
                        'IP'            =>  $dtl['ip'],
                        'COUNTRY-NAME'  =>  $dtl['country_name'],
                        'COUNTRY-CODE'  =>  $dtl['country_code'],
                        'CITY'          =>  $dtl['city']
                    );
    }

    return $stt;
}

echo var_dump( countryCityFromIP('4.2.2.4'));

----------------------------------------------------------------------------------------

and i get this result on local and hosting

array
'IP' => string '4.2.2.4' (length=7)
'COUNTRY-NAME' => string 'UNITED STATES' (length=13)
'COUNTRY-CODE' => string 'US' (length=2)
'CITY' => string 'Broomfield, CO' (length=14)

mahanhazrati
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I get my IP address using $_SERVER['REMOTE_ADDR'];

but its show UNITED STATE Country. I live in India, So how can I get the Name India.

Its Show Wrong Country Name.

sonu_smart12
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

it gives me a fatal error
Fatal error: Call to undefined function curl_init() in...

hi dear kuldeep04
i use your function.that was great but i change this function based on my need ihope this function can help php developers

function countryCityFromIP($ip){
    if(ip2long($ip)== -1 || ip2long($ip) === false){
    $stt = false ;
    }else{
    $ipDetail=array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.hostip.info/get_json.php?ip=".$ip);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $dtl = json_decode(curl_exec($ch),true);
    curl_close($ch);
    $stt = array(
    'IP' => $dtl['ip'],
    'COUNTRY-NAME' => $dtl['country_name'],
    'COUNTRY-CODE' => $dtl['country_code'],
    'CITY' => $dtl['city']
    );
    }
    return $stt;
    }
    echo var_dump( countryCityFromIP('4.2.2.4'));

so what do i do here???

asinan007
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi,If you Have Ip address you can find the country,Location,State,Latitudeand Longitude details using the online site Ip-Details.Com

srirasha
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi

The following is very simple to find country from IP Address

$ip_address=$_SERVER[REMOTE_ADDR];
$country=geoip_country_name_by_name($ip_address);

kavinesh
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0878 seconds using 2.7MB