Home  >  Article  >  Backend Development  >  How to set encoding for header in php

How to set encoding for header in php

藏色散人
藏色散人Original
2021-09-14 10:09:212644browse

How to set the encoding for the header in php: 1. Open the corresponding PHP file; 2. Set the encoding through the "header("content-type:text/html; charset=xxx");" syntax.

How to set encoding for header in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to set the encoding of the header in php?

header("content-type:text/html; charset=xxx");

The function of this function header() is to send the information in the brackets to the http header.

If the content in the brackets is as mentioned in the article, the function is basically the same as the 3f2ae36b77c0b1762e465ca019e7bd30 tag. If you compare it with the first one, you will find that the characters are similar. But the difference is that if there is this function, the browser will always use the xxx encoding you requested and will never be disobedient, so this function is very useful. Why is this so? Then we have to talk about the difference between HTTPS headers and HTML information:

The https header is a string sent by the server before transmitting HTML information to the browser using the HTTP protocol.

Because the meta tag belongs to html information, the content sent by header() reaches the browser first. The popular point is that header() has a higher priority than meta (I don’t know if this can be said). Add a php page that has both header("content-type:text/html; charset=xxx") and 1dd9d9bc813bb1594796bd2b8615c022 , the browser will only recognize the former http header and not the meta. Of course, this function can only be used within php pages.

There is also a question left, why does the former definitely work, but the latter sometimes doesn’t? This is why we want to talk about Apache next.

AddDefaultCharset

In the conf folder in the root directory of Apache, there is the entire Apache configuration document httpd.conf.

Open httpd.conf with a text editor. Line 708 (may be different in different versions) contains AddDefaultCharset xxx, where xxx is the encoding name. The meaning of this line of code: Set the character set in the https header of the web page file in the entire server to your default xxx character set. Having this line is equivalent to adding a header ("content-type: text/html; charset=xxx") to each file. Now you can understand why the browser always uses gb2312 even though the meta setting is utf-8.

If there is a header("content-type:text/html; charset=xxx") in the web page, the default character set will be changed to the character set you set, so this function will always be useful. If you add a "#" in front of AddDefaultCharset xxx, comment out this sentence, and the page does not contain header ("content-type..."), then it is the meta tag's turn to take effect.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set encoding for header in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn