$_POST字符串变量的某些字符显示错误


Some characters of $_POST string variable are wrongly displayed

我有两个php页面。在第1页提交表单后,它的已发布数据将显示在第2页。这很好,但像'"这样的一些字符会在它们之前自动获得',空格也会消失。

例如,我在第1页给出' "。这在第2页显示为'' '"。正如您所看到的,字符已附加',空格也已消失。

我的代码:

Page1.php

<html>
<head>
<title>PAGE 1</title>
</head>
<body>
    <form enctype="multipart/form-data" action="page2.php" method="post">
       <input type="text" name="txtNaam" id="txtNaam" />
       <input type="submit" value="Submit">
    </form>
</body>
</html>

Page2.php

<?php
// TEST 1
echo $_POST['txtNaam'];               // <== '' '"
echo "<br/>";   
// TEST 2
echo rawurlencode($_POST['txtNaam']); // <== %5C%27%20%20%20%20%5C%22
echo "<br/>";   
// TEST 3
echo urlencode($_POST['txtNaam']);    // <== %5C%27++++%5C%22
?>

如何在张贴这些特殊字符时使其正确显示?

试试这个:

echo stripslashes($_POST['txtNaam']);

你试过吗

echo htmlspecialchars($_POST['txtNaam'], ENT_QUOTES);

echo htmlentities(stripslashes($_POST['txtNaam']), ENT_QUOTES)

如果magic_quotes_gpc打开,PHP中的所有$_GET$_POST$_COOKIE变量(GPC)都将已经转义了"''等特殊字符。

为了防止这种情况发生,你可以禁用它。

像这样编辑你的php.ini

magic_quotes_gpc = Off

您还可以使用base64_encode()&base64_decode()