1

After I read some stuff on-line I came up with this PHP script to detect the browser's language and redirect the user to the correct website's version. Short said, if the user has a browser in Swedish, then the script should redirect to index.php, if not, then it should redirect the user to en.php.

It works fine in some computers and mobile phones and in others it blocks the website. I suppose that the script is not OK and is causing some conflict in older browsers.

So, could you please take a look at my script and tell me if I am doing anything wrong and how can I fix it?

Cheers!

<?php
include ('administration/fonts.php');
?><?php
$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
// Now we simply evaluate that variable to detect specific languages

if($lc == "sv"){
    header("location: index.php");
    exit();
}

else if($lc == "en"){
    header("location: en.php");
    exit();
}
?>

P.S. - Yes, the script is before the tag and there are no spaces between the "?>" tag and tag.

viriato
  • 859
  • 2
  • 13
  • 26

1 Answers1

2

New answer after added details from the OP.

English users should be redirected, but Swedish user should stay on this site, so we'll rewrite the code like this (I added comments with // Reeno):

<?php
include ('administration/fonts.php');

$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
    // Reeno: I added strtolower() if some browser sends upper case letters
    $lc = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
// Now we simply evaluate that variable to detect specific languages

// Reeno: Redirect all non-Swedish users to the English page (they can have "en", but also "dk", "de", "fr"...
if($lc != "sv"){
    header("location: http://www.domain.com/en.php");
    exit();
}
// Reeno: Swedish users stay on this site
?>
HTML code...

Old answer

You check for $lc == "sv" and $lc == "en" but you forgot the third case: $lc could be empty!

Rewrite the if at the end like this, so everybody with a non Swedish browser will get to en.php:

if($lc == "sv"){
    header("location: index.php");
    exit();
}
else {
    header("location: en.php");
    exit();
}
?>

btw header("location: ..."); requires an absolute URI like header("location:http://www.domain.com/en.php"); (some clients also accept relative URIs)

Reeno
  • 5,720
  • 11
  • 37
  • 50
  • No, his code is fine, because user should stay on current version if no language is present. In your case the user will be redirected always, not when necessary. – michal.hubczyk Mar 20 '14 at 15:29
  • 1
    `if the user has a browser in Swedish, then the script should redirect to index.php, if not, then it should redirect the user to en.php.` The OP always expects a redirect – Reeno Mar 20 '14 at 15:31
  • I still may be wrong :) See my comment to his question, now I think there's another problem – Reeno Mar 20 '14 at 15:33
  • I have tested your code. The website still works but now the only version I see on the browser is the English one. Before I could see the Swedish one on IE and Safari which are in Swedish... I will ask someone to test it and let you know about the results later. Cheers! – viriato Mar 21 '14 at 06:54
  • But is the file you're calling the same file you're redirecting the Swedish users to? Or is it some other file, e.g. `language.php`? Is the code you posted the complete file or is there some HTML/PHP after the last `?>`? – Reeno Mar 21 '14 at 08:03
  • @Reeno the file I am using this script in is the same that Swedish users should be redirected to, index.php – viriato Mar 21 '14 at 08:41
  • So then simply don't redirect the swedish user or you're creating an endless loop for them. I updated my answer, please see above – Reeno Mar 21 '14 at 08:47
  • Works like butter in hot croissants! :D (in other words, works perfectly!) It seems I was making it too complicated... Truly thankful!! – viriato Mar 21 '14 at 09:16