我可以使用这种消毒液安全地将数据输出给用户吗


Can I safely output data to the user using this sanitize

因此,我正在努力找出对xs进行消毒的最佳方法,以便向用户安全输出。

或多或少,当存储表单中的值时,im使用strip_tags();则bind_params();

当Im将要向用户输出数据时,Im也使用htmlenties();

数据将仅显示在<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>";

?>

是的,您可以安全地使用此代码。我看到您已经在使用bind_param(我假设是mysqliPDO库)和htmlentities,前者可以防止SQL注入(对您造成损害),后者可以防止跨站点脚本编写(对用户造成损害)。

在写入数据库之前,您甚至不需要调用strip_tags,尽管如果您根本不希望用户输入包含任何JS/PHP/HTML标记(以及如果您忘记在输出时调用safe函数),这是一个不错的主意。

当您将数据插入数据库时,必须使用mysql_real_sescape_string或使用PDO,如果显示数据,则必须使用htmlspecialchars