Home  >  Article  >  Backend Development  >  How to solve the php request garbled problem

How to solve the php request garbled problem

藏色散人
藏色散人Original
2020-10-20 09:39:402276browse

Solution to garbled php request: First open tomcat's server.xml; then set the element's attribute "URIEncoding="UTF-8"".

How to solve the php request garbled problem

Recommended: "PHP Video Tutorial"

request, response Chinese garbled problem and solution

Request garbled code refers to: the request parameters sent by the browser to the server contain Chinese characters, and the value of the request parameter obtained by the server is garbled;

response garbled code refers to: The data sent by the server to the browser contains Chinese characters, and the browser displays garbled characters;

The reason for the garbled characters: Whether it is the request garbled code or the response garbled code, it is actually caused by the client This is caused by the inconsistency between the encoding format used by the browser and the server. Take the garbled request as an example: the browser sends a request to the server. Because the communication between the browser and the server is essentially a socket stream, the request parameters (characters) must first be converted into bytes, which is the encoding process. The server receives The request parameters are decoded (bytes converted to characters) and then encapsulated into the request object. If the client's encoding is not consistent with the server's decoding, the value of the request parameter obtained through the request will be garbled.

Solution:

1. Response garbled code

The data sent by the server to the browser is encoded in accordance with ISO-8859-1 by default, and the browser receives it After receiving the data, it is decoded according to the default character set and displayed. If the default decoding character set of the browser is not ISO-8859-1, garbled characters will appear.

For garbled responses, you only need to specify an encoding character set on the server side, and then notify the browser to decode according to this character set. There are three ways:

1. A. Set the server-side encoding

response.setCharacterEncoding("utf-8");

The default is ISO-8859-1; The method must be set before response.getWriter()

B. Notify the browser of the data format sent by the server

response.setHeader("contentType", "text/html; charset=utf- 8”);

2. A. Notify the browser of the data format sent by the server

response.setContentType("text/html;charset=utf-8”);

Equivalent to response.setHeader("contentType", "text/html; charset=utf-8"); it will actually override response.setCharacterEncoding("utf-8"). During development, you only need to set

B. Set the server-side encoding

response.setContentType("text/html;charset=utf-8");

3. A. Set the server-side encoding

response.setCharacterEncoding("utf-8");

B. The browser uses utf-8 for decoding

Summary: Settings:

A , Set the server-side encoding

response.setCharacterEncoding("utf-8");

B. Notify the browser of the data format sent by the server

response.setContentType("text /html;charset=utf-8”);

C. The browser uses utf-8 for decoding

           

I set it up like this, just in case;

2. Request garbled characters

There are three ways to access from the browser: directly enter the URL in the address bar to access, click on the hyperlink in the page to access, and submit the form to access. The browser of the first access method encodes the parameters according to UTF-8 by default, and the browsers of the latter two access methods encode the parameters according to the display encoding of the current page. Therefore, for request garbled characters, you only need to set the corresponding decoding format on the server side. Due to different access methods, browsers also have different encoding formats for parameters. In order to facilitate processing, access through hyperlinks and forms must also be in UTF-8 format. That is, the encoding for displaying the current page must also use UTF-8, so browse The processor will uniformly use UTF-8 to encode parameters.

A. Post method

The post method belongs to form submission, and the parameters exist in the request body.

request.setCharacterEncoding("utf-8")

B. get method

The parameters submitted by get method will follow the uri in the request line, and the server will follow the default Decode the iso-8859-1. At this time, there are two ways to solve the garbled code:

Method 1: Modify the default encoding of the uri parameter on the server side

In tomcat's server.xml, Just set the attribute URIEncoding="UTF-8" of the element. (This attribute is not set by default)

For example: Note: 1. Set the attribute useBodyEncodingForURI="true" of the element, which means that the request body and uri use the same encoding format. By setting these two attributes, you can solve both the garbled code in the get method and the garbled code in the post method. 2. Specify that the server decodes GET and POST in accordance with UTF-8 by modifying server.xml. All web applications managed by Tomcat are required to use UTF-8 encoding, that is, all JSP and HTML pages use UTF-8 encoding.

The above is the detailed content of How to solve the php request garbled problem. 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