search
HomeBackend DevelopmentPHP ProblemWhat to do if apache php is garbled?

What to do if apache php is garbled?

Jul 06, 2020 am 10:45 AM
apachephp

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

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

E:\OpenStudy\www\Electronic Books

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

[php.ini]
default_charset = "UTF-8"

Server root directory:

E:\OpenStudy\www

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

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

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

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

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

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>

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"

2. Change the home directory language to

AddDefaultCharset  utf-8

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

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

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
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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools