-2

My goal is to write a php function:

getCountry($url) {
...
return $countryCode;
}

Is there are "good enough" way to detect the country of a website ?

I am talking about country, not the geoip of the server.

If my strategy good enough ?

if tld is local (ex: .fr)->France

else parse Header Accept-Language: en-US or parse the html page.

Maybe there is an remote API for that ? like the whois ?

yarek
  • 11,278
  • 30
  • 120
  • 219
  • If the domain doesn't tell you, and a geoip of the hosting isn't adequate, then "no"... raising the question of what your business need for this is – Mark Baker Mar 30 '14 at 11:47
  • possible duplicate of [PHP - Detect country](http://stackoverflow.com/questions/14414705/php-detect-country) – MarkP Mar 30 '14 at 11:55
  • 1
    What is "the country of the website", if not the location of its server? This is quite a non-sense here... or maybe you are talking about the website's language locale instead (en-US is a locale, not a location) ? – John WH Smith Mar 30 '14 at 11:56

2 Answers2

1

I am afraid there is some confusion here... The algorithm you're proposing is very inaccurate :

  • The TLD doesn't tell you anything about the website's country : most of the time, you do not need to be living in the TLD's country to get a domain depending from it.
  • The HTTP header Accept-Language, is, obviously, about a language, not a location. You'll only get information about the language the website's contents are written in. en-US defines American English in your example.

The country of a website is the country it is hosted in, i.e. the location of its web server. This can be retrieved by :

Yet, this might become inaccurate in some circumstances (websites hidden behind CloudFlare for instance).

If this is not what you're looking for, then I suggest you edit your question and give us more information about what you are trying to achieve...

John WH Smith
  • 2,743
  • 1
  • 21
  • 31
0

It sounds like your asking, what language is the user using in their web browser? For example, if someone in France who speaks french, would be reading their bowser in french, while a visitor to France from the US would be in France but reading their browser in english.

$userBrowserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($userBrowserLang == 'fr') {

           include("index_french.php"); //french version of index page.

 }