Php 我是否可以使用此清理将数据安全地输出给用户

Php 我是否可以使用此清理将数据安全地输出给用户,php,javascript,xss,sanitization,Php,Javascript,Xss,Sanitization,因此,我正在努力找出最好的方法来清理xss,以便安全地输出给用户 或多或少,当存储表单中的值时,我使用strip_标记();然后绑定_参数() 当我将要向用户输出数据时,我也使用htmlentities() 数据将仅显示在和标记内 例如: 来自用户的一些数据 来自用户的一些数据 这样行吗 Index.php <form action="sante.php" method="post"> Name: <input type="text" name="fname"> Age

因此,我正在努力找出最好的方法来清理xss,以便安全地输出给用户

或多或少,当存储表单中的值时,我使用strip_标记();然后绑定_参数()

当我将要向用户输出数据时,我也使用htmlentities()

数据将仅显示在
标记内

例如:

来自用户的一些数据

来自用户的一些数据

这样行吗

Index.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
(我假设是
mysqli
PDO
库),这可以防止SQL注入(对您造成损害),而
htmlentities
可以防止跨站点脚本编写(对用户造成损害)


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

在向数据库插入数据时,必须使用mysql\u real\u escape_字符串或使用PDO,
如果显示数据,必须使用htmlspecialchars

谢谢,是的,用户只允许纯文本谢谢,我使用的是mysqli bind_param/prepare
<?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>";


?>