让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 小时前
【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
大数据·数据仓库·hive·hadoop·sql·apache
SelectDB2 小时前
5000+ 中大型企业首选的 Doris,在稳定性的提升上究竟花了多大的功夫?
大数据·数据库·apache
喂完待续15 小时前
Apache Hudi:数据湖的实时革命
大数据·数据仓库·分布式·架构·apache·数据库架构
数据爬坡ing2 天前
过程设计工具深度解析-软件工程之详细设计(补充篇)
大数据·数据结构·算法·apache·软件工程·软件构建·设计语言
运维行者_2 天前
使用Applications Manager进行 Apache Solr 监控
运维·网络·数据库·网络安全·云计算·apache·solr
皓空揽月2 天前
php+apache+nginx 更换域名
nginx·php·apache
求知若渴,虚心若愚。4 天前
高可用实战之Nginx + Apache篇
运维·nginx·apache
阿里云云原生5 天前
Apache RocketMQ EventBridge:为什么 GenAI 需要 EDA?
apache·rocketmq
沈健_算法小生6 天前
Apache RocketMQ:消息可靠性、顺序性与幂等处理的全面实践
apache·rocketmq
老虎06276 天前
JavaWeb(苍穹外卖)--学习笔记17(Apache Echarts)
笔记·学习·apache