关于Apache默认编码错误,导致网站乱码的解决方案,apache编码
最近经常有同学在使用LAMP/WAMP时,遇到这样的编码错误问题:
A网站程序编码UTF-8编码安装成功,运行成功。
B网站程序编gb2312也要安装在同一服务器上。
这样就出现问题了,Apache默认编码UTF-8在解析A网站的时候没有任何问题,当运行B网站时出现的"蝌蚪文"乱码问题。
单纯的修改Apache默认编码为gb2312这样就导致A网站出现"蝌蚪文"。
问题分析:
如果你在网上搜索 “apache配置”,搜到的页面大多都会建议你在httpd.conf中加上这么一句:AddDefaultCharset GB2312。
对于新手而且是只用GB2312编码的开发人来说,这么做是ok的。但是如果要想使用UTF-8字符集的话,比如 在test.php文件中需要有 meta http-equiv="Content-Type" content="text/html; charset=UTF-8" 这段代码。
这时你再打开浏览器访问test.php页面的话,你看到的是正确的页面。但是如果实际上浏览器还是以GB2312编码解释从服务器返回的response,为什么呢?原因是浏览器是根据http应答消息头部中的 Content-type: text/html; charset=GB2312 来决定使用何种编码解释应答,也就是说apache服务器仍然用GB2312编码传递数据。
所以说如果apache的默认字符集被设置成了GB2312,即使在页面中声明使用UTF-8编码,apache服务器还是会按照GB2312编码来传送http response。没关系,我们把AddDefaultCharset GB2312 改成 AddDefaultCharset UTF-8,看看什么结果?
如果你看到乱码恭喜你,你还知道是乱码问题;如果你看到是空白页面,那么你就惨了,你可能会以为这是其他什么原因造成的,而不会从编码的角度去考虑怎么解决问题。这是为什么?原因在于php文件本身是用系统字符集来编码的,中文的windows XP都是用GB2312,每一个文件头部都有字段指示该文件是用何种方式编码的。当apache接到浏览器的请求后,会让php去解释所请求的页面,比如 test.php。php会识别出test.php的编码方式是GB2312后(就像我们用javac编译java源文件时,编译器默认用系统编码读源文件里的内容。
如果源文件不是用系统编码来保存的,可以用命令javac -encoding指定具体的编码),把数据以GB2312的编码格式传递给apache,而apache服务器不会改变从php传来的数据,只是在应答消息头部中把字符集设置成UTF-8: Content-type: text/html; charset=UTF-8. 也就是说你传递的是GB2312编码的数据,而浏览器却以UTF-8编码来解释应答消息。
由于UTF-8为3个字节表示一个汉子,而普通的GB2312或BIG5是两个。页面输出时,由于上述原因,出现半个汉字的情况,这时该半个汉字会和的>结合成一个乱码字,导致IE无法读完的话,会发现实际上整个叶面全部已经输出了。如果使用的是Mozilla、Mozilla Firefox、Sarafi的浏览器这不会造成这个问题,而是一堆乱码。这是由于Firefox浏览器和IE解析网页编码的策略不同产生的。OK,我们把test.php以UTF-8保存,再用浏览器访问时,就没有问题了。
可这样做,会使得apache目录下的所有web应用只能用同一种编码。如何搞定?
解决办法:
首先,可以使用AddDefaultCharset off来关闭默认文件编码,这样apache服务器就不会在http应答消息头部设置charset,只是设置Content-type: text/html. 而浏览器就会依靠html文件中设置的harset来决定编码。
其次,脚本php.ini文件中的default_charset = “UTF-8″作用同httpd.conf文件,把该行注释掉,使php自动识别文件的编码方式。
这样不论你用什么编码方式,只要test.php中的meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ 与你test.php文件编码方式相同,就不会产生乱码问题。用户提交数据的编码浏览器提交的字符编码由客户端的characher encoding决定。
例如,当前浏览器的编码是Gb2312,用户提交数据后,无论apache设置的编码方式是GB2312还是UTF-8,这时在服务器端接收到的仍是以Gb2312编码的数据。
如果要在返回页面上显示用户刚才提交的数据,而该页面是用UTF-8编码的或者要在数据库中存储的用户提交的数据,而数据库是UTF-8编码的,那就要做字符转换了。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

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.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools