How to solve the php request garbled problem
Solution to garbled php request: First open tomcat's server.xml; then set the element's attribute "URIEncoding="UTF-8"".
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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

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

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 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.

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 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 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
