让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 编码来解析页面的。

相关推荐
愤怒的苹果ext8 小时前
Apache Superset 使用
apache·doris·bi·superset·ai bi
qq_185198699 小时前
骆驼任务Flowable + Apache Camel 集成
spring·apache·flowable
ClickHouseDB10 小时前
ClickHouse 十年开源,今非昔比!
clickhouse·开源·apache
代码不会写1 天前
Apache Iceberg:架构原理、读写机制、性能优化与生态
性能优化·架构·apache·iceberg
Gent_倪2 天前
数据治理之安全管理:Apache Ranger
apache
SelectDB3 天前
Apache Doris 实战教程:手把手实现 ClickHouse 表结构迁移与数据校验
apache
SelectDB3 天前
Apache Doris 实战教程:从零搭建 MCP Server,让 AI Agent 直接用自然语言查数据
apache
SelectDB3 天前
Apache Doris Segment V3 宽表优化实战教程:从建表到体验元数据按需加载
apache
java_logo3 天前
Docker Compose 部署 Apache Superset:轻松搭建开源 BI 平台
docker·开源·apache·superset·轩辕镜像·superset部署方案·docker superset
Gent_倪4 天前
数据治理之元数据管理:Apache Atlas
apache