【发布时间】:2015-05-18 14:17:48
【问题描述】:
我有一个 HTML 表单,它在提交后应该将表单字段呈现到 echo 语句中的 PHP 文件中(其中还包含 HTML 元素)
我遇到的问题是,虽然结果按预期生成,但在特定情况下,当表单条目中有“或”时,它们在 results.php 页面中未转义
我的文件:
form.html
<form action="./results.php" method="post" id="sgemail">
<table align="center" border="1" width="60%" style="border-color: #D2DFF5;">
<tr>
<td width="50%" style="padding-left: 8px; text-align: left;">
<div class="form-group">
<strong>Article 1 Title<br /></strong>
<input type="text" class="form-control" id="ifn1title" placeholder="" name="ifn1title"><br />
<strong>Article 1 URL<br /></strong>
<input type="text" class="form-control" id="ifn1url" placeholder="" name="ifn1url">
</div>
</td>
<td width="50%" style="padding-left: 8px; text-align: left;">
<div class="form-group">
<strong>Article 1 Description<br /></strong>
<textarea rows="5" class="form-control" cols="10" name="ifn1desc" form="sgemail"></textarea>
</div>
</td>
</tr>
</table>
<table align="center" width="60%">
<tr>
<td align="center" width="33%" style="padding-left: 8px; text-align: left;">
<input class="btn btn-primary" type="submit" value="Generate Results HTML Code">
</td>
</tr>
</table>
</form>
results.php
<?php
if (isset($_POST['ifn1title']))
if (isset($_POST['ifn1url']))
if (isset($_POST['ifn1desc']))
{
$form_ifn1t = $_POST['ifn1title'];
$form_ifn1u = $_POST['ifn1url'];
$form_ifn1d = $_POST['ifn1desc'];
echo "
<table style=\"background-color:#D6E3F0;\" bgcolor=\"#D6E3F0\" align=\"center\" width=\"100%\">
<tr align=\"center\">
<td align=\"center\"><br />
<textarea id=\"selectori\" rows=\"50\" cols=\"120\" onclick=\"this.focus();this.select()\" readonly=\"readonly\">
<ul>
<li><a href=\"$form_ifn1u\"><strong>$form_ifn1t</strong></a><br />$form_ifn1d</li>
</ul>
</textarea>
</td>
</tr>
</table>";
}
?>
当我提交表单时,
对应位置的结果为
<ul>
<li><a href="http://www.example.com"><strong>\"Article\"</strong></a><br />\"Test Description\"</li>
</ul>
我该如何解决它,以使结果如下所示?
<ul>
<li><a href="http://www.example.com"><strong>Article</strong></a><br />Test Description</li>
</ul>
谢谢
编辑:
我在我的 php 文件上添加了以下内容,这解决了我的问题
{
$_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true);
$_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true);
$_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true);
$_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);
}
可以防止代码注入吗? 谢谢
【问题讨论】:
-
看起来您需要关闭魔术引号,但如果您遇到这个问题,您确实需要升级您的 PHP 版本,因为魔术引号大约在三年前被删除了。
-
我在 PHP 5.3.29 如何关闭这个单个文件的魔术引号
标签: php html forms stripslashes