我想保护我的网站,所以我不容易sql注入或xss。这些安全功能是否足够?
这里是我的代码:
//here's the form (abbreviated)
<form>
<label for="first_name" class="styled">First Name:</label>
<input type="text" id="first_name" name="first_name" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br />
//submit button etc
</form>
if (isset($_POST['submit'])) {
//gets rid of extra whitesapce and escapes
$first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
//check if $first_name is a string
if(!is_string($first_name)
{
echo "not string";
}
//then insert into the database.
.......
}
mysqli_real_escape_string
:我知道这个功能逃逸若干字母像\ n \ r,因此当数据被输入到DBC,那就“ \'旁边的所有逃脱的信件?
此脚本是否足以防止大多数SQL注入?只是转义并检查数据是否是字符串。对于整数值(比如用户输入价格),我只是:
is_numeric()
。我该如何使用
htmlspecialchars
?我是否应该仅在回显和显示用户数据时使用它?或者我应该在将数据插入数据库时使用它?什么时候应该使用
strip_tags
或htmlspecialchars
?
因此,所有这些功能:
if (isset($_POST['submit'])) {
//gets rid of extra whitesapce and escapes
$first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
//check if $first_name is a string
if(!is_string($first_name)
{
echo "not string";
}
//gets rid of any <,>,&
htmlspecialchars($first_name);
//strips any tags with the first name
strip_tags($first_name)
//then insert into the database.
.......
}
我应该使用哪种功能SQL注入和我应该使用哪些对XSS?
什么时候用户可以插入xss脚本对我?何时有表格?
我会建议适当的表单验证,而不是仅仅用户提供的数据。 – 2010-05-04 21:43:21
适当的表单验证是什么意思? – ggfan 2010-05-05 02:08:41
当我说适当的表单验证时,我主要想到了一些使用编程语言中构建的html表单抽象的web框架中的例子,那就是创建一个类来表示一个表单,它将知道它具有哪些字段以及如何验证它们,返回清理数据,打印出html等。在你的例子中,你可以使用正则表达式('\ w +')验证你的$ first_name,如果验证通过,你很确定你没有任何不安全的字符反斜杠等,否则你提出一个错误。 – 2010-05-05 13:18:25