2015-10-29 27 views
2

這是我的一種形式(PHP + MySQL,textarea替換爲TinyMCE)。它用段落,項目符號,標題和文本對齊(右,左,中心和正確)記錄描述。消毒輸入但輸出不如預期

enter image description here

一旦提交,如

<p style="text-align: justify;"><strong>Introduction</strong></p> 
<p style="text-align: justify;">The death of the pixel leaves you with a flowing, magazine-quality canvas to design for. A canvas where curves are curves, not ugly pixel approximations of curves. A canvas that begins to blur the line between what we consider to be real and what we consider to be virtual.</p> 
<p style="text-align: justify;">It wasn't too long ago that there was one set of rules for use of type on print and use of type on screen. Now that we have screens that are essentially print quality, we have to reevaluate these conventions.</p> 
<p style="text-align: justify;">Web sites are transforming from boring fields of Arial to embrace the gamut of typographical possibilities offered by web fonts. Web fonts, combined with the style and layout options presented by the creative use of CSS and JavaScript offer a new world of typographic oppor</p> 
<ol> 
<li style="text-align: justify;">point 1</li> 
<li style="text-align: justify;">point 2</li> 
<li style="text-align: justify;">point 3</li> 
</ol> 

,我讀了你需要的是淨化進入數據庫的任何數據,以避免XSS並開始尋找解決方案出現的記錄。

我找到的解決方案是使用「htmlspecialchars()」(來源:Lynda.com - 創建安全的PHP網站)。

因此,本教程說,我們需要保存到數據庫之前清空我們的輸入,並使用類似(示例代碼)

<?php 
    if($_SERVER['REQUEST_METHOD'] === 'POST') { 
     $category_description = $_POST['category_description']; 
     echo $category_description; 
     echo '<br><br>'; 
     echo htmlspecialchars($category_description); 
     echo '<br><br>'; 
     echo htmlentities($category_description); 
     echo '<br><br>'; 
     echo strip_tags($category_description); 

    } 
?> 

避免XSS

我知道它直到這裏。 htmlspecialchars()函數將一些預定義的字符轉換爲HTML實體,htmlentities()將字符轉換爲HTML實體,strip_tags()完全刪除任何標籤。

但結果使用htmlspecialchars(),ヶ輛(),並用strip_tags()後,輸出現在呈現爲

enter image description here

我相信這是安全的,但並不好看時取出的頭版從數據庫。

如何渲染已通過htmlspecialchars或htmlentities的輸入?

+0

你嘗試過這種回聲<<< EOD $ category_description EOD;引用http://stackoverflow.com/questions/6924193/what-is-the-use-of-eod-in-php –

+0

@PuyaSarmidani,我想我找到了一個解決方案。 –

回答

1

我的建議是建立一個函數來淨化你所有的文本輸入,並檢查所有的輸出是來自於數據庫或任何其他來源,像以下功能:

<?php 
// filter for user input 
function filterInput($content) 
{ 
    $content = trim($content); 
    $content = stripslashes($content); 

    return $content; 
} 

//filter for viewing data 
function filterOutput($content) 
{ 
    $content = htmlentities($content, ENT_NOQUOTES); 
    $content = nl2br($content, false); 

    return $content; 
} 

取決於你的戰略,您可能會向過濾器添加額外功能或刪除一些功能。但是你在這裏有什麼功能就足以保護你免受XSS的侵害。

編輯:除上述功能外,this answer也可能與您的網站保護部分相關。

參考了不同的方法:

這也是一個好主意看看下面的鏈接:

而且重要的是這是好事,知道的十大風險,更多地瞭解它。

0

我不確定這是否是正確的方法來解決問題,但我在php手冊中找到了一個函數「htmlspecialchars_decode()」。手冊說它與「htmlspecialchars()」完全相反。我試過了,效果很好。

html_entity_decode()函數與htmlentities()相反。

+0

歡迎任何確認。 –