因此,我试图找出清理 xss 以安全输出给用户的最佳方法。
或多或少,当从表单存储值时,我使用 strip_tags(); 然后 bind_params();
当我即将向用户输出数据时,我也使用 htmlentities();
数据只会显示在内部<p>
和<a>
标签中。
例如:
<p> Some data from user </p>
<a href=""> Some data from user </p>
这应该工作吗?
索引.php
<form action="sante.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
然后 sante.php
<?php
$name = $_POST["fname"];
$age = $_POST["age"];
$namn = strip_tags($name); // then storing into mysql with bind_param
$older = strip_tags($age); // then storing into mysql with bind_param
// before output, htmlentities
function safe( $value ) {
htmlentities( $value, ENT_QUOTES, 'utf-8' );
return $value;
}
// Now showing values
echo safe($namn). "<br>";
echo "<p>" .safe($older) . "</p>";
?>