Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 高手请进,无数高手完全弄不懂的编码问题.神奇的乱码

高手请进,无数高手完全弄不懂的编码问题.神奇的乱码

Jun 23, 2016 pm 01:55 PM
Garbled characters coding master

也就是此贴,http://bbs.csdn.net/topics/390807783?page=1#post-397542169

此贴有详细描述。解决此问题人可以得240分。这么个小问题,困扰二三天了,我已经从http底层看了数据包,依然无法解决。
-------------------------------------------------------------------------------------------
无数高手搞不定的(乱码)编码问题:只有在代码放入linux下用chrome访问时才乱码。其它任何情况正常。
windows下任何浏览器下无任何问题。
linux下。只有chrome访问出现乱码。(手工修改chrome编码当然可以正常显示了。)
-----------------------------------
http://parttime.wengege.com/h/login.html

响应编码居然为:gbk,utf-8.

HTTP/1.1 200 OK
Server: nginx/1.4.1
Date: Mon, 09 Jun 2014 15:28:28 GMT
Content-Type: text/html; charset=gbk,utf-8
Content-Length: 1843
Last-Modified: Mon, 09 Jun 2014 15:28:16 GMT
Connection: keep-alive
ETag: "5395d290-733"
Accept-Ranges: bytes

此/login.html内容用editplus打开看为utf-8.也已经另存为好几次为utf-8了。

是哪里让浏览器判断是gbk了呢?从而乱码?

-------------------------
引用 2 楼 changjay 的回复:
换一个编辑器试试,比如记事本或者emeditor之类的
我换了几个编辑器保存了。w3c检查里面有GBK字符。所以将utf-8识别为gbk了。真是奇怪了。无数次另存为转化过。

http://parttime.wengege.com/h/test.html
上面连接也是,在chrome下有乱码(JS引入时有乱码,奇怪的是其它部分乱码好了)。在ie下完全正常。
----------------------------------------------------
现在问题是:所有文件都检查过了(css,php,js),确定为utf-8编码。
有几个问题搞不定:
一、普通的html为utf-8编码。http响应居然为gbk,utf-8.所以出现乱码。问题是gbk哪来的?这三个字符哪来的?
我已经全站搜索GBK这三个字符。无获!!!
二、就算html偶尔成功后,但引入js还是乱码。指定引入编码为utf-8.

三、通过w3c烂网站检查,“成功识别”编码依然为 “gbk”. 然后w3c网站无数次崩溃。

真是神奇,检查结果说某行有问题。所有字符我从新打过,还是如此。换个目录的文件thinkphp3.1的登录还是正常。而thinkphp3.2下运行此html就是不正常。关键是这html和thinkphp毛关系都扯不上啊?


回复讨论(解决方案)

也许是apache的配置问题吧,记得apache配置中有字符集的设置的

又来了?
windows下任何浏览器下无任何问题。这话说的太武断!
这是 XP 360极速浏览器的截图


我不否认 IE 中为没有问题
这是因为 IE 有强大的字符集识别功能,完全可以忽视 Content-Type: text/html; charset 的影响
这就是网景倒闭的原因之一。
而那些在网景倒闭时公布的浏览器代码(10余兆c程序)基础上建立起来的各种浏览器,由于微软专利的原因,是无法解决这个问题的
当然这是题外话,不在讨论的序列中

你还是要找到在哪里会有 charset=gbk 的
不要过分相信工具软件,最好手工逐行查找。毕竟配置文件也就哪几个

但也不排除你而外安装了什么插件,造成了这种情况

此问题在我本机上完美重现
只要是login.html保存为utf-8编码无BOM,必定乱码。保存时保留BOM,侧无乱码。用UltraEdit反复测试多次,均如此。

关键是你以前的项目有没有这种情况!  如果没有那就是代码问题,如果也有,那可能是服务端问题、操作系统问题或者压根就是你代码输入的有错误(原先sql语句里错了一个字母,折腾一天,因为觉得自己不能犯小错误,实际上还是犯了!)

没什么实质性的东西,我的一点建议,主要是看看到底是代码原因,还是运行环境的原因!

此问题在我本机上完美重现
只要是login.html保存为utf-8编码无BOM,必定乱码。保存时保留BOM,侧无乱码。用UltraEdit反复测试多次,均如此。



怎么保留BOM 谢谢

检查一下nginx.conf配置文件?有没有gbk?

又来了?
windows下任何浏览器下无任何问题。这话说的太武断!
这是 XP 360极速浏览器的截图



xu大,我感觉楼主指的是windows上的服务器,既然换服务器没事,估计就是nginx哪里的配置或者模块干扰了吧?


此问题在我本机上完美重现
只要是login.html保存为utf-8编码无BOM,必定乱码。保存时保留BOM,侧无乱码。用UltraEdit反复测试多次,均如此。



怎么保留BOM 谢谢


用UltraEdit另存为,格式选 'UTF-8' 是保留,选 'UTF-8 -无BOM' 是不保留。 

不要干扰人家的判断

$url = 'http://parttime.wengege.com/h/login.html';$s = file_get_contents($url, false, null, 0, 10);//echo bin2hex($s); //3c21444f435459504520$url = 'http://parttime.wengege.com/h/test.html';$s = file_get_contents($url, false, null, 0, 10);echo bin2hex($s); //efbbbf3c21444f435459
Copy after login

显然 
3c21444f435459504520 是没有 BOM 头的
efbbbf3c21444f435459 是有 BOM 头的

BOM 头对于浏览器而言,至多会影响到显示样式,而不会造成乱码

$url = 'http://parttime.wengege.com/Public/js/search.js';
$s = file_get_contents($url, false, null, 0, 10);
echo bin2hex($s); //2f2fe6a0b9e68daee7b1

/Public/js/search.js无BOM头

BOM 头对于浏览器而言,至多会影响到显示样式,而不会造成乱码

这可说不说,服务器返回是gbk,utf8这样的编码,如果没有BOM头来说明,是按gbk显示还是按utf8显示?明显这里是按gbk来显示的。

又来了?
windows下任何浏览器下无任何问题。这话说的太武断!
这是 XP 360极速浏览器的截图


我不否认 IE 中为没有问题
这是因为 IE 有强大的字符集识别功能,完全可以忽视 Content-Type: text/html; charset 的影响
这就是网景倒闭的原因之一。
而那些在网景倒闭时公布的浏览器代码(10余兆c程序)基础上建立起来的各种浏览器,由于微软专利的原因,是无法解决这个问题的
当然这是题外话,不在讨论的序列中

你还是要找到在哪里会有 charset=gbk 的
不要过分相信工具软件,最好手工逐行查找。毕竟配置文件也就哪几个

但也不排除你而外安装了什么插件,造成了这种情况



谢谢你版主。
我的意思是代码在windows下运访问不会有任何问题。
你看到的是在linux下运行的,就是有问题。

ob_start();
header("Content-Type: text/html; charset=gbk,utf8");
echo "测试文本";
ob_flush();
?>
保存为无BOM和有BOM,就会乱码和正常

检查一下nginx.conf配置文件?有没有gbk?


又来了?
windows下任何浏览器下无任何问题。这话说的太武断!
这是 XP 360极速浏览器的截图



xu大,我感觉楼主指的是windows上的服务器,既然换服务器没事,估计就是nginx哪里的配置或者模块干扰了吧?
是的,这位兄弟正解,版主大人,熬夜太多,水平是极高的,但精神恍惚,最近回复 我问题时,理解的和我说的恰好相反。
我讲了很多次是响应,xu大说是请求,还给我列了很例子。.... 我指的windows服务器。他说是windows下访问。我说gbk是自动生成的。他说gbk是我手工设置的。..... 正好和我相反。

也许是apache的配置问题吧,记得apache配置中有字符集的设置的



同样的ngnix配置下,其它项目都没有问题,差不多代码。

经测试,保存为utf-8 +bom 可以解决此问题。但原因何在呢?有人能够解释么?

header("Content-Type: text/html; charset=gbk,utf8");
是发出回应的头

get_headers(url) 
得到的 Content-Type: text/html; charset=gbk,utf8
是服务器的响应

我什么时候说过请求啦?
相应的头不是你设置的吗?放在配置文件里自动发出,也是你设置的

我一点都没搞混,而是你自己忙晕了!
你换了服务器就正常了,这恰恰表示了出问题的服务器的配置有问题!

以前的相同的代码,还有其它一些html代码,都是没有bom头的,也没有乱码。

关键是你以前的项目有没有这种情况!  如果没有那就是代码问题,如果也有,那可能是服务端问题、操作系统问题或者压根就是你代码输入的有错误(原先sql语句里错了一个字母,折腾一天,因为觉得自己不能犯小错误,实际上还是犯了!)

没什么实质性的东西,我的一点建议,主要是看看到底是代码原因,还是运行环境的原因!


以前没有这样情况

此问题在我本机上完美重现
只要是login.html保存为utf-8编码无BOM,必定乱码。保存时保留BOM,侧无乱码。用UltraEdit反复测试多次,均如此。



是啊,兄台找到结果了。但原因何在?这也是我想知道的问题。

其实谁也没有找到原因,只是观察到一些现象
 Content-Type: text/html; charset= gbk,utf8
这个 gbk 才是真正的原因,只不过你不肯需找他的来源罢了

为什么发出gbk,不在服务器上翻,估计谁也没法说出具体原因。

xuzuning版主正解!
问题原因找到,是nginx此网站配置时使用了charset gbk,utf-8 这样设置了。去掉gbk就ok了。所以乱码解决。
是我在thinkphp网站有一个网友和我碰到同样问题。提醒我上服务器上查看配置文件解决的。

header("Content-Type: text/html; charset=gbk,utf8");
是发出回应的头

get_headers(url) 
得到的 Content-Type: text/html; charset=gbk,utf8
是服务器的响应

我什么时候说过请求啦?
相应的头不是你设置的吗?放在配置文件里自动发出,也是你设置的

我一点都没搞混,而是你自己忙晕了!
你换了服务器就正常了,这恰恰表示了出问题的服务器的配置有问题!



谢谢!!!

当然,版主也同时提醒我看服务器上配置文件了。xu大,早点提醒我就好了。

如果是ngix charset ?置??,?甚???folder放就可以呢? ??folder??也是用同一?charset的。所以之前才想是不是有.htaccess影??前folder。就是因???,?我?有?得是default charset?置??。

你说你的文件确实保存的是UTF8,好像每个页面的mate标签是可以设置浏览的编码的,会不会是设置了GBK
或者是后台代码输出的字符是GBK?

你用的是cms么?是不是从gbk转成utf8的

其实我对php不熟悉,也只是猜测  猜测哈

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

How to solve garbled word page numbers How to solve garbled word page numbers Jun 25, 2023 pm 03:23 PM

Solution to garbled word page numbers: 1. Open the word document and click the "File" option in the upper left corner; 2. Select the "More" option, and then click the "Options" button; 3. Select "Advanced" in the word options; 4. . Find "Show field codes instead of field values" in "Show document content", remove the check in front, and click OK to return to the home page.

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

How to solve tomcat startup garbled code How to solve tomcat startup garbled code Dec 26, 2023 pm 05:21 PM

Solutions to garbled tomcat startup: 1. Modify Tomcat's conf configuration file; 2. Modify the system language; 3. Modify the command line window encoding; 4. Check the Tomcat server configuration; 5. Check the project encoding; 6. Check the log file; 7 , try other solutions. Detailed introduction: 1. Modify Tomcat's conf configuration file, open Tomcat's conf directory, find the "logging.properties" file, etc.

How to solve the problem of Chinese garbled characters in Windows 10 How to solve the problem of Chinese garbled characters in Windows 10 Jan 16, 2024 pm 02:21 PM

In the Windows 10 system, garbled characters are common. The reason behind this is often that the operating system does not provide default support for some character sets, or there is an error in the set character set options. In order to prescribe the right medicine, we will analyze the actual operating procedures in detail below. How to solve Windows 10 garbled code 1. Open settings and find "Time and Language" 2. Then find "Language" 3. Find "Manage Language Settings" 4. Click "Change System Regional Settings" here 5. Check the box as shown and click Just make sure.

How to solve the problem of garbled characters in win11 system documents How to solve the problem of garbled characters in win11 system documents Jun 29, 2023 pm 06:29 PM

How to solve the problem of garbled text documents in win11? When many users use the win11 system, text documents are garbled and cannot be read normally. Many friends do not know how to solve this problem. In fact, this method is not difficult. Below, the editor has compiled the steps to solve the problem of garbled Windows 11 system documents. I hope it can bring you some inspiration! Steps to solve garbled Windows 11 system documents: 1. First, open the control panel of win11, enter control panel in the search box below, and click Search to enter the control panel. 2. After entering the panel, find the clock and area and click to enter, then click on the area option. 3. After entering, click on the management panel, and then click on Change system regional settings.

Editing method to solve the problem of garbled characters when opening dll files Editing method to solve the problem of garbled characters when opening dll files Jan 06, 2024 pm 07:53 PM

When many users use computers, they will find that there are many files with the suffix dll, but many users do not know how to open such files. For those who want to know, please take a look at the following details. Tutorial~How to open and edit dll files: 1. Download a software called "exescope" and download and install it. 2. Then right-click the dll file and select "Edit resources with exescope". 3. Then click "OK" in the pop-up error prompt box. 4. Then on the right panel, click the "+" sign in front of each group to view the content it contains. 5. Click on the dll file you want to view, then click "File" and select "Export". 6. Then you can

Solve the problem of garbled characters in win11 notepad Solve the problem of garbled characters in win11 notepad Jan 05, 2024 pm 03:11 PM

Some friends want to open a notepad and find that their win11 notepad is garbled and don't know what to do. In fact, we generally only need to modify the region and language. Win11 Notepad is garbled: First step, use the search function, search and open "Control Panel" Second step, click "Change date, time or number format" under Clock and Region Third step, click the "Manage" option above Card. The fourth step is to click "Change System Regional Settings" below. The fifth step is to change the current system regional settings to "Chinese (Simplified, China)" and click "OK" to save.

How to solve filezilla garbled characters How to solve filezilla garbled characters Nov 20, 2023 am 10:16 AM

Solutions to filezilla garbled characters include: 1. Check the encoding settings; 2. Check the file itself; 3. Check the server configuration; 4. Try other transfer tools; 5. Update the software version; 6. Check for network problems; 7. Seek technical support. To solve the problem of FileZilla garbled characters, you need to start from multiple aspects, gradually investigate the cause of the problem, and take corresponding measures to repair it.

See all articles