在PHP中顯示格式化的用戶輸入
我們將討論沒有經過過濾的輸出的危險,給出一個安全的顯示格式化輸出的方法。
沒有過濾輸出的危險
如果你僅僅獲得用戶的輸入然后顯示它,你可能會破壞你的輸出頁面,如一些人能惡意地在他們提交的輸入框中嵌入javascript腳本:
This is my comment.
<script language='javascript:
alert('Do something bad here!')'>.
這樣,即使用戶不是惡意的,也會破壞你的一些HTML的語句,如一個表格突然中斷,或是頁面顯示不完整。
只顯示無格式的文本
這是一個最簡單的解決方案,你只是將用戶提交的信息顯示為無格式的文本。使用htmlspecialchars()函數,將轉化全部的字符為HTML的編碼。
如<b>將轉變為<b>,這可以保證不會有意想不到的HTML標記在不適當的時候輸出。
這是一個好的解決方案,如果你的用戶只關注沒有格式的文本內容。但是,如果你給出一些可以格式化的能力,它將更好一些。
Formatting with Custom Markup Tags
用戶自己的標記作格式化
你可以提供特殊的標記給用戶使用,例如,你可以允許使用[b]...[/b]加重顯示,[i]...[/i]斜體顯示,這樣做簡單的查找替換操作就可以了: $output = str_replace('[b]', '<b>', $output);
$output = str_replace('[i]', '<i>', $output);
再作的好一點,我們可以允許用戶鍵入一些鏈接。例如,用戶將允許輸入[link='url']...[/link],我們將轉換為<a href='http://www.cgvv.com.cn/bcjs/21366.html'>...</a>語句
這時,我們不能使用一個簡單的查找替換,應該使用正則表達式進行替換:
$output = ereg_replace('[link='([[:graph:]]+)']', '<a href='http://www.cgvv.com.cn/bcjs/1'>', $output);
ereg_replace()的執行就是:
查找出現[link='...']的字符串,使用<a href='http://www.cgvv.com.cn/bcjs/...'> 替換它
[[:graph:]]的含義是任何非空字符,有關正則表達式請看相關的文章。
在outputlib.php的format_output()函數提供這些標記的轉換,總體上的原則是:中國網管聯盟bitsCN.com
調用htmlspecialchars()將HTML標記轉換成特殊編碼,將不該顯示的HTML標記過濾掉,然后,將一系列我們自定義的標記轉換相應的HTML標記。
請參看下面的源代碼:
<?phpfunction 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]', '<b>', $output);$output = str_replace('[/b]', '</b>', $output);/* italics */$output = str_replace('[i]', '<i>', $output);$output = str_replace('[/i]', '</i>', $output);網管bitscn_com/* preformatted */$output = str_replace('[pre]', '<pre>', $output);$output = str_replace('[/pre]', '</pre>', $output);/* indented blocks (blockquote) */$output = str_replace('[indent]', '<blockquote>', $output);$output = str_replace('[/indent]', '</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='http://www.cgvv.com.cn/bcjs/1'>', $output);$output = str_replace('[/link]', '</a>', $output);return nl2br($output);}?>
一些注意的地方:
記住替換自定義標記生成HTML標記字符串是在調用htmlspecialchars()函數之后,而不是在這個調用之前,否則你的艱苦的工作在調用htmlspecialchars()后將付之東流。網管bitscn_com
在經過轉換之后,查找HTML代碼將是替換過的,如雙引號'將成為'
nl2br()函數將回車換行符轉換為<br>標記,也要在htmlspecialchars()之后。
當轉換[links=''] 到 <a href='http://www.cgvv.com.cn/bcjs/21366.html'>, 你必須確認提交者不會插入javascript腳本,一個簡單的方法去更改[link='javascript 到 [link=' javascript, 這種方式將不替換,只是將原本的代碼顯示出來。
outputlib.php
在瀏覽器中調用test.php,可以看到format_output() 的使用情況
正常的HTML標記不能被使用,用下列的特殊標記替換它:
- this is [b]bold[/b]- this is [i]italics[/i]- 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][indent]交錯文本[/indent]
這些只是很少的標記,當然,你可以根據你的需求隨意加入更多的標記網管聯盟bitsCN@com
Conclusion
結論
這個討論提供安全顯示用戶輸入的方法,可以使用在下列程序中
留言板用戶建議系統公告BBS系統