首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"mysqli_real_escape_string“是否足以避免SQL注入或其他SQL攻击?

问"mysqli_real_escape_string“是否足以避免SQL注入或其他SQL攻击?
EN

Stack Overflow用户
提问于 2015-09-04 14:19:33
回答 1查看 13.8K关注 0票数 7

这是我的代码:

代码语言:javascript
复制
  $email= mysqli_real_escape_string($db_con,$_POST['email']);
  $psw= mysqli_real_escape_string($db_con,$_POST['psw']);

  $query = "INSERT INTO `users` (`email`,`psw`) VALUES ('".$email."','".$psw."')";

谁能告诉我它是安全的,还是容易受到SQL注入攻击或其他SQL攻击?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-05 01:38:04

能否告诉我它是否安全,或者是否容易受到SQL注入攻击或其他SQL攻击?

No.正如uri2x所说,参见SQL injection that gets around mysql_real_escape_string()。

它们将数据(您的参数)与指令( The best way to prevent SQL injection is to use prepared statements.查询字符串)分开,并且不会为数据留下任何空间来污染查询的结构。准备好的语句解决了其中的一个fundamental problems of application security。

对于不能使用预准备语句(例如LIMIT)的情况,为每个特定目的使用非常严格的白名单是保证安全性的唯一方法。

代码语言:javascript
复制
// This is a string literal whitelist
switch ($sortby) {
    case 'column_b':
    case 'col_c':
        // If it literally matches here, it's safe to use
        break;
    default:
        $sortby = 'rowid';
}

// Only numeric characters will pass through this part of the code thanks to type casting
$start = (int) $start;
$howmany = (int) $howmany;
if ($start < 0) {
    $start = 0;
}
if ($howmany < 1) {
    $howmany = 1;
}

// The actual query execution
$stmt = $db->prepare(
    "SELECT * FROM table WHERE col = ? ORDER BY {$sortby} ASC LIMIT {$start}, {$howmany}"
);
$stmt->execute(['value']);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

我假设上面的代码不会受到SQL注入的影响,即使在模糊的边缘情况下也是如此。如果您使用的是MySQL,请确保关闭模拟的准备。

代码语言:javascript
复制
$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32391315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档