这是一种安全的方式来过滤数据并防止SQL注入和其他攻击? [英] Is this a safe way to filter data and prevent SQL-injection and other attacks?

查看:110
本文介绍了这是一种安全的方式来过滤数据并防止SQL注入和其他攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于表单字段(我也使用正则表达式来单独检查每个字段。

我创建了两个简单的函数来在插入的数据输入到mysql查询之前过滤插入的数据。

  //表单过滤器
函数过滤器($ var)
{

// HTML不允许
$ var = strip_tags(trim($ var));

//检查魔术引号和stripslashes
if(get_magic_quotes_gpc())

$ var = stripslashes($ var);
}

//现在不使用它,推荐吗?
// $ var = htmlentities($ var,ENT_QUOTES);

// Escape
$ var = mysql_real_escape_string($ var);

//返回
返回$ var ;
}

然后对于id(通过URL发送)我使用了这个过滤器:

  // ID过滤器
函数idfilter($ idfilter)
{
//删除除数字以外的所有内容
$ idfilter = e reg_replace([^ 0-9],,$ idfilter);

//圆形数字
$ idfilter = round($ idfilter);

//测试输入是否确实是数字
if(!is_numeric($ idfilter)|| $ idfilter%1!= 0)
{
$ idfilter = 0;
}

//使用formfilter(上面)过滤
return filter($ idfilter);
}

是否有建议添加或删除这些简单函数?这是安全吗? $ c>和 ereg _ * 。为防止Sql注入,您应该使用准备语句(我建议使用 PDO ),为了防止XSS,您应该使用 strip_tags()因为你在做。


I created two simple functions to filter inserted data before it's entered into a mysql query.

For formfields (I am also using regular expressions to check each field individually.

// Form filter
function filter($var) 
{               

    // HTML is not allowed
    $var = strip_tags(trim($var)); 

    // Check magic quotes and stripslashes
    if(get_magic_quotes_gpc())
    { 
        $var = stripslashes($var);
}

    // Not using it right now, is it recommended?
    // $var = htmlentities($var, ENT_QUOTES);

    // Escape
    $var = mysql_real_escape_string($var);

    // Return    
    return $var; 
}

Then for id's (sent in the URL) I am using this filter:

// ID filter
function idfilter($idfilter)
{
// Delete everything except numbers
$idfilter = ereg_replace("[^0-9]", "", $idfilter);

// Round numbers
$idfilter = round($idfilter);

// Test if the input is indeed a number
if(!is_numeric($idfilter) || $idfilter % 1 != 0)
{
    $idfilter = 0;
}

// Filter using the formfilter (above)
return filter($idfilter);
} 

Are there suggestions to add or strip from these simple functions? And is it "safe"?

解决方案

You're using deprecated function as magic_quotes and ereg_*. To prevent Sql injection you should use prepared statement (I suggest to use PDO) and to prevent XSS you should use strip_tags() as you're doing.

这篇关于这是一种安全的方式来过滤数据并防止SQL注入和其他攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆