我使用 Bootstrap 制作了一个 html 表单。我使用“必需”来确保在某些字段中填充数据以提交表单。此表单转到打开数据库连接的 php 脚本,根据提交的表单输入值,指向感谢页面并关闭连接。
这是我的第一手编码形式,我担心的是安全性。我如何阻止黑客/垃圾邮件发送者?
请您指出我的代码存在的问题,以便我在发布之前解决这些问题。请温柔点,我是一个新手,大约有 3 个月的编码经验。
请注意原始表格实际上有 9 个字段,但我假设这些详细信息对于本次讨论来说不是必需的,因此省略了它。
HTML代码
<form class="form-horizontal" method="post" action="vacancy.php">
<div class="form-group">
<label for="company" class="col-sm-3 control-label">Company Name *</label>
<div class="col-sm-6">
<input type="text" name="company" class="form-control" placeholder="Enter Company Name" required />
</div>
</div>
<div class="form-group">
<label for="contactperson" class="col-sm-3 control-label">Contact Person *</label>
<div class="col-sm-6">
<input type="text" name="contactperson" class="form-control" placeholder="Enter Full Name" required />
</div>
</div>
<div class="form-group">
<label for="designation" class="col-sm-3 control-label">Designation *</label>
<div class="col-sm-6">
<input type="text" name="designation" class="form-control" placeholder="Enter Designation" required />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Clear</button>
</div>
</div>
</form>
PHP代码
<?php
$con=mysqli_connect("localhost","db2u","password","db2");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO vacancy (Company, ContactPerson, Designation)
VALUES
('$_POST[company]','$_POST[contactperson]','$_POST[designation]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: thankyou.html");
mysqli_close($con);
?>
编辑:看来我需要验证。我正在查看 jqBootstrapValidation。甚至考虑 htmlspecialchars(哪个更容易)。我相信两者都会做得同样好,对吗?目前 PDO 对我来说有点太多了。
最佳答案
您的代码可能会被 SQL 注入(inject)破坏 http://en.wikipedia.org/wiki/SQL_injection .
尝试使用 htmlspecialchars或者更好地使用 PDO而不是旧的和危险的 mysql_* 函数。
关于php - Bootstrap HTML + MySQL PHP 表单安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21644880/