Answer the question
In order to leave comments, you need to log in
Why doesn't Microsoft Edge detect the encoding?
Cookies contains a json string with UTF-8 encoding. When viewing cookies through the debugger (F12), the encoding is correct.
When outputting to the screen via php, the encoding breaks, but if you check the encoding of this string using mb_detect_encoding(), then the encoding is UTF-8. HTML also has UTF-8 encoding.
Screenshot from the debugger. (F12)
Screen output (The last line is the result of mb_detect_encoding($_COOKIES['suggestions']))
Screen html
The code that this all outputs
var_dump($_COOKIE['suggestion']);
echo '<br>' . mb_detect_encoding($_COOKIE['suggestion']);
$organization = json_decode($_COOKIE['suggestion']);
var_dump($organization);
echo '<hr>';
echo mb_detect_encoding($organization);
echo '<hr>';
echo json_last_error_msg();
Answer the question
In order to leave comments, you need to log in
By poking new encodings into this unfortunate browser, I found that, for browsers from Microsoft, you need to use windows-1251.
UPD: lied.
The method by which php checks the encoding does not work correctly. (mb_detect_encoding()). At random, I found out the real encoding - windows-1251. And through iconv changed the encoding of cookies to UTF-8
The answer to the sub-question is 1:
Because it json_decode()
returned null , but it mb_detect_encoding(null)
will return ASCII . And yet, it's better to do it json_decode($_COOKIE['suggestion'], true)
because inside is an associative array!
In the script before var_dump
tryheader('Content-Type: text/plain; charset=utf-8');
This is because you are viewing the server's response in the browser instead of viewing it in the original. Of course , it seems that the browser shows what was sent to it, at the level of philistine experience it is, but as a developer you must remember that the browser is a very complex system and its task is to render the sent html pages. You are trying to get the browser to interpret what is essentially your debug log output without bothering to explain to the browser what you sent it. Yes, this is convenient, but by doing so, you are unconsciously relying on browser heuristics - this is precisely what leads to the effect you describe.
More specifically, your mistake is that you did not use any of the possible options to see the data "as is" and did not tell the browser what kind of data you are passing to it.
Offhand you could:
header('Content-Type: text/plain; charset=utf-8');
header('Content-Type: text/html; charset=utf-8');
error_log($data,3,__DIR__.'/debug.log');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question