让Apache正确处理不同编码的文件避免中文乱码

安装了apache2.4.39以后,默认编码是UTF-8,不管你文件是什么编码,统统按这个来解析,因此 GB2312编码文件内的中文将显示为乱码。

php 复制代码
<!doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=GB2312"> 
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="renderer" content="webkit">
	<meta name="force-rendering" content="webkit">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />	
	<meta http-equiv="Content-Language" content="zh-cn">
    </head>
    <body>	
	<span>中文测试</span> <hr/>
<?php
    header('Content-type:text/html;charset=GB2312');
    // $input_filed = '<input type="text" name="username" value="'.$_POST['username'].'">';
    // echo $input_filed;
    $input_name = '<input type = "text" name = username value = "'.$_POST['username'].'">';
    echo '请输入姓名:' . $input_name;
    echo '<hr/>';
	echo "中文测试";
    // //Example #1 $_POST 范例
    // echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
?>

</body>
</html>

解决方案:

打开apache配置文件 \Apachexxx\conf\httpd.conf ,查找AddDefaultCharset,查看设置的是啥,改为

AddDefaultCharset off

如果配置文件中没有AddDefaultCharset的话就加上一句

AddDefaultCharset off

改完了记得重启Apache

这样,Apache就能按照文件中设定的编码来正确解析文件了,前提是你的文件存储格式必须与设定编码一致。

如果不设置 AddDefaultCharset ,那么新安装完Apache后,默认都是用 UTF-8 编码来解析页面的。

相关推荐
万岳科技1 天前
教育培训小程序如何构建线上线下一体化教学体系
小程序·apache
yyuuuzz1 天前
云服务器软件部署的几个常见问题
运维·服务器·开发语言·网络·云计算·php·apache
分布式存储与RustFS1 天前
Apache Iceberg数据湖轻量化搭建:基于Rust开源存储方案
开源·apache·iceberg·rustfs·ai存储·ai memory·s3 table
睡不醒男孩0308232 天前
中启乘数 CLup 6.x Apache Doris 存算一体集群管理技术文档
apache·doris·clup
Geek_Vison2 天前
APP集成了50多个小程序后,如何搭建一个小程序管理平台来管理这些小程序~
小程序·uni-app·apache·mpaas·小程序容器
解决问题no解决代码问题2 天前
漏洞详解|CVE-2026-44825 Apache Solr 隐藏默认账号漏洞(附检测+修复全套方案)
apache·solr·lucene
BBWEYY终身尊贵会员3 天前
教培小机构小程序开发:从技术选型、系统架构到表结构与接口设计的完整实践
apache
前端 贾公子4 天前
小程序蓝牙打印探索与实践(下)
小程序·apache
DolphinScheduler社区4 天前
Apache DolphinScheduler 3.4.2 正式发布!新增 Amazon EMR Serverless 插件,增强监控与补数据能力
大数据·云原生·serverless·apache·海豚调度·版本发版
前端 贾公子4 天前
小程序蓝牙打印探索与实践(中)
apache