K
K
Konstantin Khairov2016-05-07 23:54:29
PHP
Konstantin Khairov, 2016-05-07 23:54:29

How to solve problem with UTF8 With BOM and header()?

Hello dear toasters. The other day I ran into this problem.
To simplify, I wrote the following code

<?PHP  
session_start();
if (isset($_SESSION["login"]))
echo "Добро пожаловать .$_SESSION["login"];
else {
    header("location: panel.html");
    
}


?>

I did it in the Visual Studio Code editor, when saving in UTF 8, they exit
R”RѕR±СЂРѕ RїRѕR¶R°R”RѕРІР°С‚СЊ уважаемый

If you save in UTF8 With BOM, the text will be normal, but session gives an error
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at O:\home\test1.ru\www\admin.php:1) in O:\home\test1 .ru\www\admin.php on line 2

I tried to change the encoding in two Visual Studio Code editors, Notepad++ doesn't help. I did similar code but with Header() and the situation is the same. With UTF8 header works fine but text
R”RѕR±СЂРѕ RїRѕR¶R°R”RѕРІР°С‚СЊ уважаемый

When changing to UTF 8 With BOM, the header does not work and gives an error
Warning: Cannot modify header information - headers already sent by (output started at O:\home\test1.ru\www\panel.php:1) in O:\home\test1.ru\www\panel.php on line 21

Moreover, it complains about the line <?PHP.
Any ideas what could be the problem? Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-05-08
@Kaylos

1) Always store/output everything in UTF-8 only (this includes files with code, database entries, etc.)
2) Never use BOM anywhere - this is a crooked thing that WILL create a lot of trouble for you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question