Home Backend Development PHP Problem How to solve the php request garbled problem

How to solve the php request garbled problem

Oct 20, 2020 am 09:39 AM
request

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x How to use the urllib.request.urlopen() function to send a GET request in Python 3.x Jul 30, 2023 am 11:28 AM

How to use the urllib.request.urlopen() function in Python3.x to send a GET request. In network programming, we often need to obtain data from a remote server by sending an HTTP request. In Python, we can use the urllib.request.urlopen() function in the urllib module to send an HTTP request and get the response returned by the server. This article will introduce how to use

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to encapsulate Vue3 Axios interceptor into request file How to encapsulate Vue3 Axios interceptor into request file May 19, 2023 am 11:49 AM

1. Create a new file called request.js and import Axios: importaxiosfrom'axios'; 2. Create a function called request and export it: This will create a function called request and export it Set up a new Axios instance with a base URL. To add timeout settings in a wrapped Axios instance, you can pass the timeout option when creating the Axios instance. exportconstrequest=axios.create({baseURL:'https://example.

The role and significance of Request in PHP The role and significance of Request in PHP Feb 27, 2024 pm 12:54 PM

The role and significance of Request in PHP In PHP programming, Request is a mechanism for sending requests to the Web server. It plays a vital role in Web development. Request is mainly used to obtain data sent by the client, such as form submission, GET or POST request, etc. Through Request, the data input by the user can be obtained, and the data can be processed and responded to. This article will introduce the role and significance of Request in PHP and give specific code examples.

What is request in PHP What is request in PHP Jun 01, 2023 am 10:12 AM

Request in PHP refers to request. It is a super global variable in PHP. It is used to collect data submitted by HTML forms and parameters in URLs. It can obtain data from GET and POST requests at the same time. Note that $_request is an associative array. , where the keys are the names of the form fields and the values ​​are the values ​​of the form fields. When using the $_request variable, user-entered data should always be validated and filtered to avoid security issues.

How to use context to implement request parameter verification in Go How to use context to implement request parameter verification in Go Jul 22, 2023 am 08:23 AM

How to use context to implement request parameter verification in Go Introduction: During the back-end development process, we often need to verify the request parameters to ensure the legality of the parameters. The Go language provides the context package to handle request context information. Its elegant design and simple use make it a commonly used tool. This article will introduce how to use Go's context package to implement request parameter verification and give corresponding code examples. Introduction to the context package In Go, the context package is used to deliver

How to use the urllib.request module to send HTTP requests in Python 3.x How to use the urllib.request module to send HTTP requests in Python 3.x Jul 30, 2023 am 11:21 AM

How to use the urllib.request module to send HTTP requests in Python3.x. In the actual development process, we often need to send HTTP requests to interact with the server. Python provides the urllib.request module, which is one of the modules in the Python standard library for handling URL requests. In this article, we will learn how to send HTTP requests using the urllib.request module

See all articles