如果插入用户输入而不修改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)
可以采取哪些措施来防止这种情况发生?
我正在运行PHP脚本,并继续收到如下错误:
注意:未定义的变量:第10行的C:\ wamp\www\mypath\index.php中的my_variable_name
注意:第11行的未定义索引:my_index C:\ wamp\www\mypath\index.php
第10行和第11行看起来像这样:
echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];
Run Code Online (Sandbox Code Playgroud)
这些错误消息的含义是什么?
他们为什么突然出现?我曾经使用这个脚本多年,我从来没有遇到任何问题.
我该如何解决?
这是一个一般参考问题,供人们链接到重复,而不是一遍又一遍地解释这个问题.我觉得这是必要的,因为关于这个问题的大多数现实世界的答案都非常具体.
相关元讨论:
嘿,这是我的用户注册表(register_hirer.php),其中我正在尝试输入员工的详细信息但不起作用.它在行之后给出一个解析错误:if(isset($ _ POST ['submit']),它只有一个{.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>The Freelance World</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body>
<?php
include"include/connection.php";
if (isset($_POST['submit'])
{
mysql_real_escape_string($_POST['username']);
$checkusername=mysql_query("SELECT * FROM employer WHERE eusername='{$_POST['username']}'");
if (mysql_num_rows($checkusername)==1)
{
echo "username already exist";
}
else
{
$query = "insert into employer(efname,elname,egender,eemail,eusername,epwd,eadd,ephone,ecity,ecountry) values ('".$_POST['first_name']."','".$_POST['last_name']."','".$_POST['gender']."','".$_POST['email']."','".$_POST['username']."','".$_POST['password']."','".$_POST['address']."','".$_POST['phone']."','".$_POST['city']."','".$_POST['country']."')";
$result = mysql_query($query) or die (mysql_error());
echo " Thanks for registration";
}
}
?>
<form name="register_hirer" method="post" action="register_hirer.php" >
<pre><strong>First Name</strong> <input type="text" name="first_name" > </pre>
<pre><strong>Last Name </strong> <input …Run Code Online (Sandbox Code Playgroud)