Home php教程 php手册 php 应用乱码问题解决方法集

php 应用乱码问题解决方法集

Jun 21, 2016 am 08:50 AM
charset header html

 

1) 使用 标签设置页面编码

这个标签的作用是声明客户端的浏览器用什么字符集编码显示该页面,xxx 可以为 GB2312、GBK、UTF-8(和 MySQL 不同,MySQL 是 UTF8)等等。因此,大部分页面可以采用这种方式来告诉浏览器显示这个页面的时候采用什么编码,这样才不会造成编码错误而产生乱码。但是有的时候我们会发现有了这句还是不行,不管 xxx 是哪一种,浏览器采用的始终都是一种编码,这个情况我后面会谈到。

请注意, 是属于 HTML 信息的,仅仅是一个声明,仅表明服务器已经把 HTML 信息传到了浏览器。

2) header(“content-type:text/html; charset=xxx”);

这个函数 header() 的作用是把括号里面的信息发到 http 标头。如果括号里面的内容为文中所说那样,那作用和 标签基本相同,大家对照第一个看发现字符都差不多的。但是不同的是如果有这段函数,浏览器就会永远采用你所要求的 xxx 编码,绝对不会不听话,因此这个函数是很有用的。为什么会这样呢?那就得说说 http 标头和 HTML信息的差别了:

http 标头是服务器以 http 协议传送 HTML 信息到浏览器前所送出的字串。而 标签是属于 HTML 信息的,所以 header() 发送的内容先到达浏览器,通俗点就是 header() 的优先级高于 (不知道可不可以这样讲)。假如一个 php 页面既有header(“content-type:text/html;charset=xxx”),又有,浏览器就只认前者 http 标头而不认 meta 了。当然这个函数只能在 php 页面内使用。

同样也留有一个问题,为什么前者就绝对起作用,而后者有时候就不行呢?这就是接下来要谈的Apache 的原因了。

3) AddDefaultCharset

Apache 根目录的 conf 文件夹里,有整个 Apache 的配置文档 httpd.conf。

用文本编辑器打开 httpd.conf,第 708 行(不同版本可能不同)有 AddDefaultCharset xxx,xxx为编码名称。这行代码的意思:设置整个服务器内的网页文件 http 标头里的字符集为你默认的 xxx字符集。有这行,就相当于给每个文件都加了一行 header(“content-type:text/html; charset=xxx”)。这下就明白为什么明明 设置了是 utf-8,可浏览器始终采用 gb2312 的原因。

如果网页里有 header(“content-type:text/html; charset=xxx”),就把默认的字符集改为你设置的字符集,所以这个函数永远有用。如果把 AddDefaultCharset xxx 前面加个”#”,注释掉这句,而且页面里不含 header(“content-type…”),那这个时候就轮到 meta 标签起作用了。

下面列出以上的优先顺序:

header(“content-type:text/html; charset=xxx”)

.. AddDefaultCharset xxx

..

如果你是 web 程序员,建议给你的每个页面都加个header(“content-type:text/html;charset=xxx”),这样就可以保证它在任何服务器都能正确显示,可移植性也比较强。

4) php.ini 中的 default_charset 配置:

php.ini 中的 default_charset = “gb2312″ 定义了 php 的默认语言字符集。一般推荐注释掉此行,让浏览器根据网页头中的 charset 来自动选择语言而非做一个强制性的规定,这样就可以在同台服务器上提供多种语言的网页服务。



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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles