加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – “mysqli_real_escape_string”是否足以避免SQL注入或其

发布时间:2020-12-13 21:54:31 所属栏目:PHP教程 来源:网络整理
导读:这是我的代码: $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."')"; 有人可以告诉我它是否安全,或
这是我的代码:

$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攻击?

解决方法

Could someone tell me if it is secure or if it is vulnerable to the SQL Injection attack or other SQL attacks ?

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

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

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

// 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,请确保你转动模拟准备.

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读