通过网页表单提交的变量,在任何使用之前都需要进行清理/消毒,以防止各种恶意企图。
技巧 #1
function clean($value) {
// If magic quotes not turned on add slashes.
if(!get_magic_quotes_gpc())
// Adds the slashes.
{ $value = addslashes($value); }
// Strip any tags from the value.
$value = strip_tags($value);
// Return the value out of the function.
return $value;
}
$sample = "<a href='#'>test</a>";
$sample = clean($sample);
echo $sample;
这是一个不错的开始,但它远不像今天 PHP 使用中那样高效。
请查看 htmlspecialchars() 和/或 htmlentities(),stripslashes() 以及(对于数据库用户)mysqli_real_escape_string()
示例用法
我只建议在输出时使用它。
如果您要提交到数据库(例如发布评论),请转义您的数据!!
当然,我推荐使用 PDO 而不是 mysqli_*() 函数,因为它们会自动转义(为了更好的描述)。
对于今天的 PHP 版本,有没有什么解决方案?
谢谢