[英]how to properly secure form - flat file database
我的表單將用戶輸入(輸入+ textarea)保存到平面文件數據庫中。 我發現了很多關於如何創建平面文件數據庫的例子,但沒有人能夠正確地介紹如何從XSS和其他惡意攻擊中正確保護表單的一些好基礎知識。
我知道最好的方法是讓(Ex :)成為一個SQL數據庫......但事實並非如此。
到目前為止,我知道(這可能是錯的!如果是的話,糾正我):
<?php ...data... ?>
)而不是.txt文件 deny from all
數據庫文件夾中的deny from all
刪除帶有deny from all
的.htaccess mysql_real_escape_string
但足夠ffdb 你怎么看? 我感謝您的幫助
Dunno你在哪里得到它,但通過使用
- .php文件存儲數據(內部)而不是.txt文件
你可以肯定它會允許任何人他們想要的任何攻擊,
- 從數據庫文件夾中的所有內容中刪除.htaccess
完全沒有道理
所以,似乎唯一的問題是
- 如何從XSS正確保護表單
它通過使用htmlspecialchars()
解決
這是我很久以前在一個遙遠的星系中寫的這樣一個腳本的例子......
如果看起來不清楚,請隨時詢問。
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
// iterating over POST data
foreach($_POST as $key => $value) {
//first we are doing non-destructive modifications
//in case we will need to show the data back in the form on error
$value = trim($value);
if (get_magic_quotes_gpc()) $value = stripslashes($value);
$value = htmlspecialchars($value,ENT_QUOTES);
$_POST[$key] = $value;
//here go "destructive" modifications, specific to the storage format
$value = str_replace("\r","",$value);
$value = str_replace("\n","<br>",$value);
$value = str_replace("|","¦",$value);
$msg[$key] = $value;
}
//various validations
$err='';
if (!$msg['name']) $err.="You forgot to introduce yourself<br>";
if (!$msg['notes']) $err.="You forgot to leave a comment!<br>";
//and so on
//...
// if no errors - writing to the file
if (!$err) {
$s = $msg['name']."|".$msg['email']."|".$msg['notes']."|".time()."\n";
$fp = fopen("gbook.txt","a");
fwrite($fp,$s);
fclose($fp);
//and then redirect
Header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
//otherwise - show the filled form
} else {
//if it was not a POST request
//we have to fill variables used in form
$_POST['name'] = $_POST['email'] = $_POST['notes'] ='';
}
?>
<html>
<head></head>
<body>
<? if ($err): ?><font color=red><b><?=$err?></b></font><? endif ?>
<form method="POST">
Name: <input type="text" name="name" value="<?=$_POST['name']?>"><br>
Email: <input type="text" name="email" value="<?=$_POST['email']?>"><br>
Notes: <textarea rows="3" cols="30" name="notes"><?=$_POST['notes']?></textarea><br>
<input type="submit" name="submit">
</form>
</body>
</html>
它會產生這樣的所謂管道分隔格式
name1|email1|comment
name2|email2|comment
你可以用file()+ explode()來讀它
如果將內容存儲在文本文件中,則無需擔心轉義字符串。
您可以將其過濾為惡意HTML,但這取決於您對內容的處理方式。
確保該文件位於公共目錄之外的文件夾中,或者使用deny from all
htaccess技巧中的deny from all
保護,並確保使用文件鎖定以防止它同時被覆蓋。
此外,如果您正在尋找一個好的平面文件數據庫,請查看Flintstone ,它是我寫的一個鍵/值數據庫存儲,可能對您有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.