【问题标题】:Unexpected difference in posted encodings between WAMP and LAMP php environments (Â detected)WAMP 和 LAMP php 环境之间发布的编码出现意外差异(检测到)
【发布时间】:2020-10-02 17:42:06
【问题描述】:

我 [大部分] 成功地从 WAMP PHP 环境迁移到 LAMP 环境并且一切正常,除了我发现当我将特殊字符发布到 php 时,然后在 WAMP 上我得到发布的结果,但在 LAMP 上,我的结果中出现了一些 Â 字符)。

我有一个简单的php演示页面如下:

<?php

echo "<html>";
echo "<head>";
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
echo "</head>";
echo "<body>";

$testcol="test2";
if ( isset($_POST[$testcol])) {
    //echo "<LI>".str_replace("[", "<BR>[",print_r(getallheaders(), true));
    echo "<LI>".getallheaders()['Content-Type'];
    echo "<LI> POST Encoding = ".mb_detect_encoding($_POST[$testcol], 'UTF-8', true);
    echo "<LI> _POST   = ".htmlentities($_POST[$testcol], ENT_QUOTES)."<HR>";

    $fn=ini_get('error_log').'.encoding_test.txt';
    file_put_contents($fn, $_POST[$testcol]);

    echo "Result stored in $fn<HR>";
}

$test="\"<lOR+EMØ>&"."'£$"."¥©Ȏ4$"."€";

echo "<LI>adjusted=".htmlspecialchars($test, ENT_QUOTES)."<BR>";

echo "<HR>";

echo "
<form method='post' action='" . $_SERVER['SCRIPT_NAME'] . "'>
    
    <input type='text' name='test1' value='".addslashes($test)."'>
    <textarea name='test2'>$test</textarea>
    <input type='submit' value='Submit'>
    
</form>";

echo "</body>";
echo "</html>";
?>

当我运行它并点击 WAMP 环境中的 [提交] 按钮** - 然后查看我看到的 encoding_test.txt 文件:

"<lOR+EMØ>&'£$¥©Ȏ4$€

但是当我在 LAMP 上运行它并查看 encoding_test.txt 文件时,我看到了:

"<lOR+EMÃ>&'£$¥©È4$â¬

为什么会这样?是否有服务器设置或我缺少的东西?

谢谢 安倍

** 请注意,在浏览器上 - WAMP 和 LAMP 之间的一切看起来都一样 - 我只是在服务器端看到了不同

附言。两次运行都显示“POST Encoding = UTF-8” 聚苯乙烯。我在 Apache/2.4.6 (CentOS) PHP/7.0.33 上运行,并比较了工作 LAMP 和我自己的 LAMP 之间的 php.ini 文件和 Apache 服务器信息(通过“SetHandler server-info”)和结果显示没有明显差异(文件夹/文件名等除外)

【问题讨论】:

  • 在检查其他环境(WAMPS 和 LAMPS)时,我发现了混合的结果。没有明确的模式。

标签: php apache character-encoding


【解决方案1】:

所以我能够管理这种差异的唯一方法是在使用之前重新编码结果,

$fn=ini_get('error_log').'.encoding_test2.txt';
$v=$_POST[$testcol];
$v=htmlentities($v, ENT_QUOTES);
$v=mb_convert_encoding($v, "HTML-ENTITIES","UTF-8");
$v=str_replace('"', "&quot;", $v);
$v=str_replace("'", "&apos;", $v);
$v=str_replace("&#039;", "&apos;", $v);
file_put_contents($fn, $v);

这就是说 - 我确实找到了根本问题: 我使用 Putty 读取 LAMP 文件,使用 Notepad++ 读取 WAMP 文件;但事实证明,腻子是问题所在——在腻子配置选项下——我的“接收的数据假定为哪个字符集:”的窗口翻译设置设置为“ISO-8859-1 ...”,将其更改为“UTF-8”解决了这个问题。

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多