如果插入用户输入而不修改SQL查询,则应用程序容易受到SQL注入的攻击,如下例所示:
$unsafe_variable = $_POST['user_input'];
mysql_query("INSERT INTO `table` (`column`) VALUES ('$unsafe_variable')");
Run Code Online (Sandbox Code Playgroud)
这是因为用户可以输入类似的内容value'); DROP TABLE table;--,查询变为:
INSERT INTO `table` (`column`) VALUES('value'); DROP TABLE table;--')
Run Code Online (Sandbox Code Playgroud)
可以采取哪些措施来防止这种情况发生?
是否有一个catchall函数适用于清理SQL注入和XSS攻击的用户输入,同时仍允许某些类型的html标记?
为什么in_array()有时表现得如此奇怪并且会产生如此意外的结果?
我们来看几个例子:
$arrayWithTrue = ['Andreas', 'Philipp', true];
$arrayWithNull = [1, 2, 3, null];
$arrayWithMinusOne = [-1];
var_dump(in_array('Gary', $arrayWithTrue)); // returns bool(true)
var_dump(in_array(0, $arrayWithNull)); // returns bool(true)
var_dump(in_array(true, $arrayWithMinusOne)); // returns bool(true)
Run Code Online (Sandbox Code Playgroud)
咦?这里发生了什么事!?
(几年前,我开始对这种奇怪的行为感到疑惑.我认为这可能对某些人有用,因此我输入了这个问题.)
使用PHP和MySQLi我有一个简单的表单,有4个HTML 5下拉选择列表输入.现在想知道我还需要使用Prepared Statement来保护我的数据库吗?我还有SQL注入问题的风险吗?或者是否存在使用此类输入的任何其他类型的风险.谢谢
我试图了解何时应该在 php/mysqli 中使用准备好的语句。每个 php/mysqli 查询是否应该使用准备好的语句,还是仅使用涉及用户输入的查询和实例……例如要求用户输入数据以在数据库中进行搜索的 html 表单?
我正在将旧的 php5/mysql 代码迁移到 php7/mysqli。我有很多查询 mysql 数据库的 php 文件。如果我需要对连接到 mysql 数据库的每个 php 文件使用准备好的语句,我想澄清一下...例如通过“php require”引用的 php 文件,并包含简单的 sql select 语句来渲染图像和链接到 html页?
<?php
//establish connection
$con = new mysqli('localhost','uid','pw','db');
//check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//search variable that stores user input
$search = "%{$_POST['search']}%";
//prepare, bind and fetch
$stmt = $con->prepare("SELECT image, caption FROM `tblimages`
WHERE catid = 3 AND caption LIKE ? order by caption ASC");
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->bind_result($image,$caption); …Run Code Online (Sandbox Code Playgroud)