mysqli mysql_real_escape_string_“ mysqli_real_escape_string”是否足以避免SQL注入或其他SQL攻击?...

小编典典

有人可以告诉我它是否安全或是否容易受到SQL Injection攻击或其他SQL攻击吗?

防止SQL注入的最佳方法是使用准备好的语句。它们将数据(您的参数)与指令(SQL查询字符串)分开,并且不会为数据留下任何空间污染查询的结构。准备好的语句解决了应用程序安全性的基本问题之一。

对于无法使用准备好的语句(例如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);

2020-05-17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值