Home Backend Development PHP Problem What to do if apache php is garbled?

What to do if apache php is garbled?

Jul 06, 2020 am 10:45 AM
apache php

The solution to Apache php garbled code: first comment out the code in "php.ini"; then change the home directory language to "AddDefaultCharset utf-8"; then change the encoding method of the required folder; finally Just restart apache.

What to do if apache php is garbled?

Solution to PHP display garbled characters and apache internal encoding problem

Encoding problem

The default encoding of PHP is utf-8. During the test, for the convenience of not writing the meta tag of the html code, there will be no garbled characters

E:\OpenStudy\www\CSphp
Copy after login

And the help document I downloaded The html version of electronic data requires gb2312 settings

E:\OpenStudy\www\Electronic Books
Copy after login

When starting to configure the server, PHP uses the utf-8 encoding method that is recommended by everyone

[php.ini]
default_charset = "UTF-8"
Copy after login

Server root directory:

E:\OpenStudy\www
Copy after login

Related information to be consulted

AddDefaultCharset 指令
说   明 当应答内容是text/plain或text/html时,在HTTP应答头中加入的默认字符集
语   法 AddDefaultCharset On|Off|charset
默认值 AddDefaultCharset Off
作用域 server config, virtual host, directory, .htaccess
覆盖项 FileInfo
状   态 核心(C)
模   块 core
Copy after login

If and only if the response content is text/plain or text/html, this command will add the default value in the HTTP response header. character set. Theoretically this will override the character set specified via the tag in the document body, but actual behavior usually depends on the user's browser settings. AddDefaultCharset Off will disable this feature. AddDefaultCharset On will enable Apache's internal default character set, iso-8859-1. You can also specify to use another charset from one of the IANA registered character set names. For example:

AddDefaultCharset utf-8
Copy after login

AddDefaultCharset should only be used in the following situations: all text resources use the same certain character set, and it is very troublesome to mark their character sets separately. One such example is adding character set parameters to resources containing dynamic content (such as legacy CGI scripts), which may lead to cross-site scripting attacks by including user-supplied data in the output. But please note: A better solution is to modify or delete these scripts, because setting the default character set will disable the browser's automatic character set detection function.

AddCharset 指令
说明 在给定的文件扩展名与特定的字符集之间建立映射
语法 AddCharset charset extension [extension] ...
作用域 server config, virtual host, directory, .htaccess
覆盖项 FileInfo
状态 基本(B)
模块 mod_mime
Copy after login

The AddCharset directive establishes a mapping between a specific file extension and a specific character set. charset is the MIME character set parameter of the file with extension as the extension. This mapping relationship will be forcibly added to all existing mapping relationships and overwrite all existing extension extension mappings.

AddLanguage ja .ja
AddCharset EUC-JP .euc
AddCharset ISO-2022-JP .jis
AddCharset SHIFT_JIS .sjis
Copy after login

With the above definition, the document xxxx.ja.jis will be regarded as a Japanese document using the character set ISO-2022-JP (the same is true for the document xxxx.jis.ja). In addition to informing the client of the character set encoding of the document so that it can be translated and displayed correctly, the AddCharset directive is also used for content negotiation (selecting one of several documents to return to the user based on the user's preference information).

<Directory> 指令
说   明 封装一组指令,使之仅对文件空间中的某个目录及其子目录生效
语   法 <Directory directory-path> ... </Directory>
作用域 server config, virtual host
状   态 核心(C)
模   块 core
Copy after login

and are used to encapsulate a set of instructions so that they only take effect on a certain directory and its subdirectories. Any directive that can be used in the "directory" scope can be used. Directory-path can be a full path to a directory, or a wildcard string containing Unix shell matching syntax. In a wildcard string, "?" matches any single character, and "*" matches any sequence of characters. You can also use "[]" to determine character ranges. None of the above wildcard characters can match the "/" character. So will not match /home/user/public_html, but will match correctly. For example:

<Directory /usr/local/httpd/htdocs> 
  Options Indexes FollowSymLinks 
</Directory>
Copy after login

Be careful when using the directory-path parameters: they must be consistent with the file system path used by Apache to access files. Directives assigned to a specific will not take effect on the same directory file pointed to by a different path, such as a path generated through another symbolic link.

Solution

1. Comment out the code in php.ini

[php.ini]
;default_charset = "UTF-8"
Copy after login

2. Change the home directory language to

AddDefaultCharset  utf-8
Copy after login

Change the encoding method of the required folder [E:/OpenStudy/www/Electronic Books]

<Directory "E:/OpenStudy/www/Electronic Books">
  AddDefaultCharset gb2312
</Directory>
Copy after login

The modification is completed, restart apache, and you’re done

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of What to do if apache php is garbled?. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles