PHP中實現支援顯示格式化的使用者輸入

來源:互聯網
上載者:User

標籤:http   java   使用   檔案   io   for   

你可以在這個頁面下載這個文檔附帶的檔案,也可以在檔案下載中的字元處理中下載這個文檔描述如何安全顯示的有格式的使用者輸入。我們將討論沒有經過過濾的輸出的危險,給出一個安全的顯示格式化輸出的方法。 

沒有過濾輸出的危險 

如果你僅僅獲得使用者的輸入然後顯示它,你可能會破壞你的輸出頁面,如一些人能惡意地在他們提交的輸入框中嵌入javascript指令碼: 

This is my comment. 
<script language="javascript: 
alert(‘Do something bad here!‘)">. 

這樣,即使使用者不是惡意的,也會破壞你的一些HTML的語句,如一個表格突然中斷,或是頁面顯示不完整。 


只顯示無格式的文本 

這是一個最簡單的解決方案,你只是將使用者提交的資訊顯示為無格式的文本。使用htmlspecialchars()函數,將轉化全部的字元為HTML的編碼。 

如<b>將轉變為<b>,這可以保證不會有意想不到的HTML標記在不適當的時候輸出。 
這是一個好的解決方案,如果你的使用者只關注沒有格式的常值內容。但是,如果你給出一些可以格式化的能力,它將更好一些。 
Formatting with Custom Markup Tags 
使用者自己的標記作格式化 

你可以提供特殊的標記給使用者使用,例如,你可以允許使用...加重顯示,...斜體顯示,這樣做簡單的尋找替換操作就可以了: $output = str_replace("", "<b>", $output); 
$output = str_replace("", "<i>", $output); 

再作的好一點,我們可以允許使用者鍵入一些連結。例如,使用者將允許輸入[link="url"]...[/link],我們將轉換為<a href="">...</a>語句 

這時,我們不能使用一個簡單的尋找替換,應該使用Regex進行替換: 
$output = ereg_replace(‘\[link="([[:graph:]]+)"\]‘, ‘<a href="\\1">‘, $output); 

ereg_replace()的執行就是: 
尋找出現[link="..."]的字串,使用<a href="..."> 替換它 
[[:graph:]]的含義是任何非Null 字元,有關Regex請看相關的文章。 


在outputlib.php的format_output()函數提供這些標記的轉換,總體上的原則是: 
調用htmlspecialchars()將HTML標記轉換成特殊編碼,將不該顯示的HTML標記過濾掉, 
然後,將一系列我們自訂的標記轉換相應的HTML標記。 
請參看下面的原始碼: 
<?php 


function format_output($output) { 
/**************************************************************************** 
* Takes a raw string ($output) and formats it for output using a special 
* stripped down markup that is similar to HTML 
****************************************************************************/ 

$output = htmlspecialchars(stripslashes($output)); 

/* new paragraph */ 
$output = str_replace(‘[p]‘, ‘<p>‘, $output); 

/* bold */ 
$output = str_replace(‘‘, ‘<b>‘, $output); 
$output = str_replace(‘‘, ‘</b>‘, $output); 

/* italics */ 
$output = str_replace(‘‘, ‘<i>‘, $output); 
$output = str_replace(‘‘, ‘</i>‘, $output); 

/* preformatted */ 
$output = str_replace(‘[pre]‘, ‘<pre>‘, $output); 
$output = str_replace(‘[/pre]‘, ‘</pre>‘, $output); 

/* indented blocks (blockquote) */ 
$output = str_replace(‘

‘, ‘<blockquote>‘, $output); 
$output = str_replace(‘

‘, ‘</blockquote>‘, $output); 

/* anchors */ 
$output = ereg_replace(‘\[anchor="([[:graph:]]+)"\]‘, ‘<a name="\\1"></a>‘, $output); 

/* links, note we try to prevent javascript in links */ 
$output = str_replace(‘[link="javascript‘, ‘[link=" javascript‘, $output); 
$output = ereg_replace(‘\[link="([[:graph:]]+)"\]‘, ‘<a href="\\1">‘, $output); 
$output = str_replace(‘[/link]‘, ‘</a>‘, $output); 

return nl2br($output); 
} 

?> 

一些注意的地方: 

記住替換自訂標籤產生HTML標記字串是在調用htmlspecialchars()函數之後,而不是在這個調用之前,否則你的艱苦的工作在調用htmlspecialchars()後將付之東流。 

在經過轉換之後,尋找HTML代碼將是替換過的,如雙引號"將成為" 

nl2br()函數將斷行符號分行符號轉換為<br>標記,也要在htmlspecialchars()之後。 

當轉換[links=""] 到 <a href="">, 你必須確認提交者不會插入javascript指令碼,一個簡單的方法去更改[link="javascript 到 [link=" javascript, 這種方式將不替換,只是將原本的代碼顯示出來。 

outputlib.php 
在瀏覽器中調用test.php,可以看到format_output() 的使用方式 

正常的HTML標記不能被使用,用下列的特殊標記替換它: 

- this is bold 
- this is italics 
- this is [link="http://www.phpbuilder.com"]a link[/link] 
- this is [anchor="test"]an anchor, and a [link="#test"]link[/link] to the anchor 

[p]段落 
[pre]預先格式化[/pre] 

交錯文本



這些只是很少的標記,當然,你可以根據你的需求隨意加入更多的標記 

Conclusion 
結論 

這個討論提供安全顯示使用者輸入的方法,可以使用在下列程式中 

留言板 
使用者建議 
系統公告 
BBS系統   

詳細說明:http://php.662p.com/thread-343-1-1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.