我想保護我的網站,所以我不容易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