我正在使用 PHP 和 AJAX 开发搜索索引,以使其功能强大。 当我使用 burpsuit 或其他安全扫描仪扫描它时,AJAX 代码中出现 SQL 注入(inject),但我找不到任何解决方案。代码如下:
<?php
require_once 'Connections/connect.php';
if($_GET['type'] == 'mobile'){
$result = mysql_query("SELECT mobilep FROM dictionary where mobilep LIKE '".$_GET['name_startsWith']."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
array_push($data, $row['mobilep']);
}
echo json_encode($data);
}
?>
最佳答案
这非常糟糕...您正在使用已弃用的 mysql 适配器。
使用 pdo 和绑定(bind),这是一个完整的原型(prototype):
class MySql
{
private $sDbName = '';
private $sUsername = '';
private $sPassword = '';
private $sHost = '';
private $oConnection = null;
public function __construct()
{
$this->oConnection = new PDO(
'mysql:host='
. $this->sHost
. ';dbname='
. $this->sDbName,
$this->sUsername,
$this->sPassword
);
}
public function getDb()
{
return $this->oConnection;
}
}
$aReturn[ 'data' ] = '';
if( !empty( $_GET[ 'type' ] )
&& ( !empty( $_GET[ 'name_startsWith' ] )
&& ( $_GET['type'] == 'mobile' )
)
{
$oMySql = new MySql;
$oDb = $oMySql->getDb();
$sSql = "SELECT mobilep FROM dictionary where mobilep LIKE :name";
$aBinds[ ':name' ] = $_GET[ 'name_startsWith' ] . '%';
$oStmp = $oDb->prepare( $sSql );
$oMySql->bindVariables( $oStmp, $aBinds );
$oStmp->execute();
$oResults = $oStmp->fetchall();
if( !empty( $oResults ) )
{
// var_dump( $aResults );
$oErrors = $oStmp->errorInfo();
// var_dump( $oErrors );
$aReturn[ 'data' ] = $aResults;
}
}
$sJson = json_encode( $aReturn, 1 );
header( 'Content-type', 'application/json' );
echo $sJson;
关于java - AJAX 请求中可能存在 SQL 注入(inject)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185840/