好吧,所以我已經學會了一點PHP,並嘗試做一個簡單的應用程序,但我不知道是我的網頁安全從xss和其他此類攻擊。如何保護網站免受xss(跨網站腳本)
我的PHP代碼
<?php
$title=$keywords=$description="";
$valid_er="";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST['title'])){
$valid_er="has-error";
}
else{
$title="<title>".test($_POST["title"])."<title>";
}
$keywords='<meta name="keywords" content="'.test($_POST["keywords"]).'" />';
$description='<meta name="description" content="'.test($_POST['description']).'" />';
}
function test($ci){
$ci=htmlentities($ci);
$ci=stripcslashes($ci);
return $ci;
}
?>
和我的HTML表單
<form method='post' class='form-group' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label> Your Title </label> <input placeholder="Your websites title" type="text" name="title" class='form-control' class='form-group-item'/></br>
<label> Keywords </label> <input placeholder="Your keywords separated by comma " type="text" name="keywords" class='form-control' class='form-group-item'/></br>
<label>Description </label> <textarea placeholder="A nice description about your website;" name="description" class='form-control'></textarea></br>
<input type="submit" class='btn btn-info'>
</form>
我只是想知道我是不是容易受到跨站點腳本,因爲我不認爲只有使用
htmlspecialchars()
會保護我。
請參閱http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php – christophetd
如果你能告訴我是我的代碼安全,這將是非常有益的 –
你不需要使用stripcslashes來轉義HTML。 htmlspecialchars就夠了。此外,當您在PHP代碼中編寫HTML時,不應使用轉義字符,如'<',因爲它們將被打印在瀏覽器中而不是被解釋。基本上:當你寫HTML的時候,一般寫下來;當您顯示來自用戶輸入的內容時,請使用htmlspecialchars。這有幫助嗎? – christophetd