0

I have several domains that point to the same site, some of them ending in ".br" (domain for Brazil, thus for portuguese speakers)

I want to detect from what domain the person came (.br or not) and load the correct landuage...

I can use PHP, JavaScript or standard HTML/CSS etc... How I do it? (and with what?)

speeder
  • 6,197
  • 5
  • 34
  • 51

3 Answers3

4

On the server side, use the HTTP_HOST variable which is basically the Host header and a fool-proof way of checking the host the request was sent to.

$_SERVER['HTTP_HOST']

See this question for a nice comparison between SERVER_NAME and the HTTP_HOST variables.

On the client side, use document.domain. For this page - https://developer.mozilla.org/en/document.domain, the value of document.domain is

"developer.mozilla.org"
Community
  • 1
  • 1
Anurag
  • 140,337
  • 36
  • 221
  • 257
1

$_SERVER['HTTP_REFERER'] should get that information. But this is not a sure fire way. Some people have the referrer turned off or spoofed in their browsers etc. This is the only way that I would know how, unless you can append get data to the urls on the domain to set the language etc. Then you just check for that get data.

Jim
  • 18,673
  • 5
  • 49
  • 65
1

If you are on PHP5.3+ you can use

Locale::acceptFromHttp — Tries to find out best available locale based on HTTP "Accept-Language" header

If not, you can still determine it from Accept-Language header yourself. Using the Accept Header should be somewhat more reliable than using the TLD, especially if you also need to use any of the other intl extensions.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Please keep in mind that intl is a [PEAR extension](http://pecl.php.net/package/intl) and has to be installed seperately. It does not come built in to PHP. **Accept-Language** is the best way to determine a user's language, regardless of whether you get it from an extension or read the header yourself in `$_SERVER` – Charles Jul 25 '10 at 23:07
  • @Charles `intl` is a native extension as of PHP5.3 and does no longer need to be installed separately. – Gordon Jul 26 '10 at 06:31
  • That doesn't seem to be the case with the distribution of 5.3 I have here. I'm not sure whether that's because the packages I'm using weren't compiled with it, or whether I'm missing the underlying ICU library. – Charles Jul 26 '10 at 06:55
  • It looks like the person that built the distro did so with "--enable-intl=shared" and packaged it separately. Irritating, I wish they'd leave the default compile options alone. How am I supposed to get a consistent environment if they keep changing the rules? Argh! Therefore, I stand by my original statement, modified: `intl` might not be available if your package maintainer futzed with the build options, but there's always PECL. – Charles Jul 26 '10 at 07:01