PHP | geoip_country_code_by_name() Function

The geoip_country_code_by_name() is an inbuilt function in PHP which helps to generate the two-letter country code (Each country is assigned a two-letter country code. For Example: US for United States). The function takes the hostname or IP Address as an argument and generates the two letter country code.

Syntax :

string geoip_country_code_by_name ( string $hostname )

    Parameters : The function geoip_country_code_by_name() accepts a single parameter as mentioned above and explained below.

  • $hostname : This is the only parameter accepted by the above function . This is the IP address or the hostname which the function accepts and returns the two letter country code .

Return Values : It returns a two letter ISO country code on success otherwise returns FALSE on Failure.

Below programs illustrate the geoip_country_code_by_name() function :



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP code implementing the
// geoip_country_code_by_name() function 
  
// The function takes the hostname
// 'www.example.com' as an argument
$country = geoip_country_code_by_name('www.example.com');
  
if ($country) {
    // displays the two letter ISO country code
    echo 'This host is located in: ' . $country
                                                  
}
  
?>

chevron_right


Output:

This host is located in: US

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP code implementing the
// geoip_country_code_by_name() function 
  
// The function takes the hostname
// 'www.example.com' as an argument
$country = geoip_country_code_by_name('www.geeksforgeeks.org');
  
if ($country) {
    // displays the two letter ISO country code
    echo 'This host is located in: ' . $country
                                                  
}
  
?>

chevron_right


Output:

This host is located in: US

Reference : http://php.net/manual/en/function.geoip-country-code-by-name.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.