hal*_*sed 2 php filter sanitize
我目前正在使用此过程来清理/过滤用户输入的注释 - >
这个用于去除斜线...和
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
然后注释通过此函数来清理数据...
function my_strip_tags($str) {
$strs=explode('<',$str);
$res=$strs[0];
for($i=1;$i<count($strs);$i++)
{
if(!strpos($strs[$i],'>'))
$res = $res.'<'.$strs[$i];
else
$res = $res.'<'.$strs[$i];
}
return strip_tags($res);
}
Run Code Online (Sandbox Code Playgroud)
在此之后,它使用预准备语句直接进入数据库.
function add_comment($comment,$type,$update_id,$user_id){
$query="INSERT INTO comment_updates (updateid,userid,comment) VALUES(?,?,?)";
if($stmt=$this->conn->prepare($query)) {
$stmt->bind_param('sss',$update_id,$user_id,$comment);
$stmt->execute();
if($this->conn->affected_rows==1){
$stmt->close();
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道这是否足够安全,或者他们是否还有其他更好的选择......谢谢
在考虑将数据存储到数据库时,最重要的是逃避它; 使用mysql_real_escape_string,或mysqli_real_escape_string,或PDO::quote取决于您正在使用的数据库(或oracle/pg/...的其他功能)
另一个解决方案是使用预准备语句(参见mysqli::prepare和/或PDO::prepare- 旧mysql_*扩展不支持),它将处理您所在位置的数据转发;-)
在考虑HTML输出时,您有两个解决方案:
htmlentities或htmlspecialchars:不一定看起来不错,但输出看起来像用户的输入.我会选择第一个或最后一个解决方案; 你的感觉更"危险" - 但这只是一种感觉^^ (一般的想法是"不要重新发明轮子")
| 归档时间: |
|
| 查看次数: |
2991 次 |
| 最近记录: |