0

I am struggling with code that redirects a user to other pages based on language detection. I found this code which looks promising as so far I have not had any luck from other posts on this website. My only question is related to the first line of code. What do I put in the "" part on first line?

<?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 == "fr"){
    header("location: index_french.php");
    exit();
} else if($lc == "de"){
    header("location: index_german.php");
    exit();
}
?>
<h2>Hello Default Language User</h2>
<h3><?php echo "Your 2-letter Code is: ".$lc; ?></h3>

When I run this code I get an error message:

Warning: Cannot modify header information - headers already sent by (output started at /home/m3418630/public_html/sumoresources/index.php:3) in /home/m3418630/public_html/sumoresources/index.php on line 12

Can anyone explain why this happens?

thanks

armani
  • 111
  • 1
  • 1
  • 3
  • I would ask what var_dump)substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)); returns – Royal Bg Aug 08 '13 at 07:48
  • The code that you have, defaults to `en` if other languages are not detected. You basically leave `$lc = "";` the way it is. That code file should be named `index.php`, if you haven't already done so. At least that is what I think from my findings at [**Site 1**](http://webbucket.org/php-language-detection-and-redirect-handle-user-according-to-language-code/) **and** [**Site 2**](http://expressionengine.stackexchange.com/questions/6156/how-can-i-redirect-a-homepage-depending-on-the-browser-language-settings) – Funk Forty Niner Aug 08 '13 at 08:01
  • what the `""` is that, it's "blank", and if other languages do not match the set criterias `fr` and `en`, and someone comes in from Russia `ru`, then because it's not in your conditions, Russian visitors will see the English content, being your `index.php` file which you will need to set. I would say something to the affect of `if($lc == ""){ header("location: index_english.php"); exit();` but I couldn't be completely certain about this theory, because it would be difficult for me to test it, and finding 3/4 people whose language differ from the other. Give it a try and see. – Funk Forty Niner Aug 08 '13 at 08:06
  • You may have better luck at using a JS-based script, or try finding another PHP script similar to the one you have. That's the best I can come up with. Good Luck. – Funk Forty Niner Aug 08 '13 at 08:11
  • My guess is that you have content over top your PHP, either whitespace, HTML, or a space before your ``..., or ``... etc. Check to see if you do. That is why you're getting the error message of "`headers already sent`".
    – Funk Forty Niner Aug 08 '13 at 14:19
  • You would be better off using this: http://stackoverflow.com/a/3770616/1415724 that way if a language can't be detected, it will default to English. – Funk Forty Niner Aug 08 '13 at 14:39

3 Answers3

0

You don't put anything there, it's just initiating the variable as it says in the comment.

If you're getting a headers already sent error, that means your outputting information to the page before sending header(), you can't do this without buffering using ob_start(), ob_end_flush().

JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
  • 1
    This, my friend, could be in a comment. – Kees Sonnema Aug 08 '13 at 07:46
  • when I run the code, i get this error Warning: Cannot modify header information - headers already sent by (output started at /home/m3418630/public_html/sumoresources/index.php:3) in /home/m3418630/public_html/sumoresources/index.php on line 12 – armani Aug 08 '13 at 07:56
  • @KeesSonnema I considered it but ... 'My only question is related to the first line of code. What do I put in the "" part on first line?' my answer directly answers this. – JohnnyFaldo Aug 08 '13 at 08:01
  • @armani Check your code and tell me if this is LINE 12 >>> `} else if($lc == "de"){` <<< If not, then that tells me that you're doing an `include` or you have a whitespace above or right before ` – Funk Forty Niner Aug 08 '13 at 14:34
  • @JohnnyFaldo I don't know why anyone would have downvoted your answer. It's obvious the OP does not have or hardly has any form of programming/scripting experience, and in answering vague questions as posted by the OP, one tends to fall in a trap, as it were. I will upvote your answer. My guess is that the OP has a whitespace before his opening tag, and/or HTML, or probably even a byte order mark; we can't tell for sure. All we can do is try and offer help. I've given suggestions, but the OP never responded, so I will sit back and see what comes of this. Cheers, *peace*. – Funk Forty Niner Aug 08 '13 at 14:50
  • @KeesSonnema Personally, I think it's a rather probable cause to the OP's script problem. If you are the person who downvoted this answer, then may I suggest that you give a reason. It is a credible probability, that headers are being sent before output, and that could be caused by a number of reasons, most often being a whitespace next to, or above the ` – Funk Forty Niner Aug 08 '13 at 14:54
  • @Fred I did not downvote you. I just told you that this could be in a comment, but then you explained. :) – Kees Sonnema Aug 09 '13 at 07:29
  • 1
    @KeesSonnema Good morning Kees. That is why I said "if you are the person", without laying actual blame on someone in particular. My Mama taught me that (diplomacy) cheers (*Peace*) – Funk Forty Niner Aug 09 '13 at 12:20
0

In your code snippet, you have to set a default language to the variable $lc. It will be overwritten if the server sends a language code from the current request.

Stefan
  • 21
  • 3
0

This snippet can detect language from user's browser

    $locale = null;
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {   
            $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

            foreach ($languages as $lang) {

                $lang = str_replace('-', '_', trim($lang));

                if (false === strpos($lang, '_')) {

                        $locale = strtoupper($lang);
                } else {

                    $lang = explode('_', $lang);

                    if (count($lang) == 3) {
                        $locale = strtolower($lang[0]) . ucfirst($lang[1]) . strtoupper($lang[2]);
                    } else {
                        $locale = strtolower($lang[0]) . strtoupper($lang[1]);
                    }

                }
            }
        }
echo $locale;

see http://codepad.viper-7.com/F1XfU5

Shushant
  • 1,625
  • 1
  • 13
  • 23