M
M
Mansur052020-03-18 10:37:41
PHP
Mansur05, 2020-03-18 10:37:41

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)
5e71cc62b9eaf144774627.png

Screen output (The last line is the result of mb_detect_encoding($_COOKIES['suggestions']))
5e71cc9a03bce832110243.png

Screen html
5e71cce2d11b9739798282.png

The code that this all outputs

var_dump($_COOKIE['suggestion']);
echo '<br>' . mb_detect_encoding($_COOKIE['suggestion']);


Sub-question - 1:
Going further, when decoding json, the encoding changes to ASCII, why?
$organization = json_decode($_COOKIE['suggestion']);
var_dump($organization);
echo '<hr>';
echo mb_detect_encoding($organization);
echo '<hr>';
echo json_last_error_msg();


Conclusion
5e71ceb5c1bcf601370677.png

At the same time, if I open the same file in another browser, for example, firefox, then the result of the entire script will be:
5e71ce7e46242370622084.png
It is strange that there are no such errors in the output of Edge.

Why is this happening?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mansur05, 2020-03-18
@Mansur05

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

A
AUser0, 2020-03-18
@AUser0

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!

A
Alexander, 2020-03-18
@Seasle

In the script before var_dumptryheader('Content-Type: text/plain; charset=utf-8');

F
Flying, 2020-03-18
@Flying

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:

  1. Report that you are sending text data, not html, viaheader('Content-Type: text/plain; charset=utf-8');
  2. Indicate that you are sending data in utf-8 encoding viaheader('Content-Type: text/html; charset=utf-8');
  3. Look at the source text, not at the page render (Ctrl + U, it seems)
  4. Just output the data to the text log viaerror_log($data,3,__DIR__.'/debug.log');

F
FanatPHP, 2020-03-18
@FanatPHP

Question marks in diamonds mean that the string is not in utf-8, which is
strange since jason doesn't understand other encodings.
You're doing some really weird perversions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question