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

相关推荐
zhojiew15 小时前
基于Pyspark对Apache Iceberg核心功能的使用实践
apache
SeaTunnel1 天前
AI 让 SeaTunnel 读源码和调试过时了吗?
大数据·数据库·人工智能·apache·seatunnel·数据同步
http阿拉丁神猫2 天前
ansible脚本解读
服务器·ansible·apache
小羊Yveesss2 天前
商家小程序外卖订单打印方案:云打印机对接、分单逻辑与模板配置实战
小程序·apache
@insist1232 天前
信息安全工程师-Apache/IIS安全增强与OWASP漏洞防护
安全·apache·软考·信息安全工程师·软件水平考试
Geek_Vison3 天前
如何借助小程序容器技术实现跨端APP的敏捷开发
小程序·apache·敏捷流程
Apache RocketMQ3 天前
Apache RocketMQ 5.0 架构解析:如何基于云原生架构支撑多元化场景
云原生·架构·apache·rocketmq·java-rocketmq
RingWu3 天前
微服务架构-全链路追踪:Apache SkyWalking
微服务·架构·apache
Donk_674 天前
高可用-Keepalived 解析
运维·服务器·apache
小羊Yveesss6 天前
门店小程序外卖配送搭建实战:配送对接、运费策略与多门店调度方案
小程序·apache