2016-05-05 114 views
-3

我有一個textarea,用戶可以在其中添加一些東西。爲避免插入HTML,我在textarea上使用以下PHP functions檢查輸入中是否有HTML

$text = addslashes(strip_tags(htmlspecialchars($_POST['message']))); 

來自用戶的消息看起來像下面的數據庫。

8KsAtP <a href="http://lqexajgwyrsk.com/">lqexajgwyrsk</a>, 
[url=http://imndawriqhnk.com/]imndawriqhnk[/url], 
[link=http://qyozfozrqier.com/]qyozfozrqier[/link], 
http://oykrvybeqata.com/ 

我的問題是,我怎麼能檢查有HTML代碼textarea的,而不是在數據庫中插入它像上面我想顯示錯誤消息。

我的問題與How to prevent XSS with HTML/PHP?不同的是,我詢問如何檢查字段中是否存在html或鏈接。並回答這個問題,我如何檢查也是由戴爾提供。而this question正在詢問如何預防。我已經使用在this answer.

+0

這是錯誤的順序'strip_tags'將一無所有脫光因爲'htmlspecialchars'將已經轉換所有標籤到他們的實體。在輸出中使用'htmlspecialchars',並對引號進行編碼。 – chris85

+0

http://php.net/manual/en/function.htmlspecialchars.php和http://php.net/manual/en/function.htmlentities.php –

回答

0

給定功能要直接回答你的問題:

// check there is no html content 
if(strip_tags($_POST['message']) == $_POST['message']) { 
    // continue to process 
} else { 
    // there is html in the message 
}