<分区>
Possible Duplicate:
PHP: the ultimate clean/secure function
我今天修改了我网站的安全过滤器。我过去常常过滤输入而不对输出做任何事情。
这里是:
所有用户输入的变量根据类型通过这两个函数:
PS:由于我没有从头开始编码,所以我对所有变量都进行了编码,包括那些未在查询中使用的变量。我知道这是一个性能 killer ,并且会消除它。安全总比后悔好,对吧?
// numbers (I expect very large numbers)
function intfix($i)
{
$i = preg_replace('/[^\d]/', '', $i);
if (!strlen($i))
$i = 0;
return $i;
}
// escape non-numbers
function textfix($value) {
$value = mysql_real_escape_string($value);
return $value;
}
XSS 预防:
输入 - 过滤用户提交的文本,如帖子和消息。如您所见,它目前是空的。不确定是否需要 strip_tags。
输出 - 在所有 html 输出上
function input($input){
//$input = strip_tags($input, "");
return $input;
}
function output($bbcode){
$bbcode = textWrap($bbcode); // textwrap breaks long words
$bbcode = htmlentities($bbcode,ENT_QUOTES,"UTF-8");
$bbcode = str_replace("\n", "<br />", $bbcode);
// then some bbcode (removed) and the img tag
$urlmatch = "([a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+)";
$match["img"] = "/\[img\]".$urlmatch."\[\/img\]/is";
$replace["img"] = "<center><img src=\"$1\" class=\"max\" /></center>";
return $bbcode;
}
我包含了 img 标签,因为它可能容易受到 css 的攻击...
你怎么看?有什么明显的错误吗?够好了吗?