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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – “mysqli_real_escape_string”是否足以避免SQL注入或其他SQL攻击?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码

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

解决方法

正如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);

脚本宝典总结

以上是脚本宝典为你收集整理的php – “mysqli_real_escape_string”是否足以避免SQL注入或其他SQL攻击?全部内容,希望文章能够帮你解决php – “mysqli_real_escape_string”是否足以避免SQL注入或其他SQL攻击?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。