Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the Encoding of header in php

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to set the header code in php. For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Php header setting encoding method: 1, open the corresponding PHP file; 2, through "header (" content-type:text/html; charset=xxx ");" syntax setting encoding can be.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How to set the encoding for header in php?

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

The function header () sends the information in parentheses to the http header.

If the content in parentheses is like what is said in the article, the function is basically the same as the label, and we find that the characters are similar to the first one. But the difference is that if there is this function, the browser will always use your required xxx encoding, absolutely will not be disobedient, so this function is very useful. What causes it? Let's talk about the difference between HTTPS headers and HTML messages:

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

Because the meta tag belongs to html information, the content sent by header () reaches the browser first, and the common point is that header () takes precedence over meta (I don't know if that's possible). Add header ("content-type:text/html; charset=xxx") to a php page, and browsers will recognize only the former http header rather than meta. Of course, this function can only be used within php pages.

There is also a question: why does the former absolutely work while the latter sometimes does not? That's why we're going to talk about Apache.

AddDefaultCharset

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

Open httpd.conf with a text editor, and line 708 (different versions may vary) has AddDefaultCharset xxx,xxx as the encoded name. The meaning of this line of code: set the character set in the https header of the web file throughout the server to your default xxx character set. Having this line is equivalent to adding a line of header ("content-type:text/html; charset=xxx") to each file. Now you can see why browsers always use gb2312 when meta is set to utf-8.

If there is a header ("content-type:text/html; charset=xxx") in the page, change the default character set to the character set you set, so this function is always useful. If you add a "#" before AddDefaultCharset xxx, comment out this sentence, and the page does not contain header ("content-type …") At this time, it is time for the meta tag to work.

This is the answer to the question about how to set the code for header in php. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report