今天偶然看了一篇博文,说是一道php面试笔试题,原文如下:
请找出下面代码中的问题,修复并优化
100个。 //注册新用户,要求用户名与email不能与以前的重复。 $mysqli = new Mysqli($host, $user, $pass); for ($i=0; $i
特意看了一下,是2013年的文章,所以不做过多评论,(mysqli连接少了一个参数,原文如此)。该题给出的答案并不太令人满意,通过这道题让我想到的是,应该怎么做才能尽量避免SQL注入?
使用mysqli批处理技术,bind_param()
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted.\n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", $mysqli->affected_rows);
/* close connection */
$mysqli->close();
?>
参数说明:
i corresponding variable has type integer 整数
d corresponding variable has type double 浮点型小数
s corresponding variable has type string 字符串
b corresponding variable is a blob and will be sent in packets 二进制包
原理说明:为什么bind_param()可以防止SQL注入?
使用了占位符(“?”):无论传多少值都是安全的,因为她已经被定义成一个参数,而非一条语句的部分;
预编译模式:在数据库层已经被编译成特定的执行方式,如select、insert等,不会因为后续的参数而改变执行方式;
内部自动转义特殊字符。
