PHP 防範xss攻擊

XSS 全稱爲 Cross Site Scripting,用戶在表單中有意或無心輸入一些惡意字符,從而破壞頁面的表現!php

看看常見的惡意字符XSS 輸入:html

1.XSS 輸入一般包含 JavaScript 腳本,如彈出惡意警告框:<script>alert("XSS");</script>安全

2.XSS 輸入也多是 HTML 代碼段,譬如:xss

(1).網頁不停地刷新 <meta http-equiv="refresh" content="0;">網站

(2).嵌入其它網站的連接 <iframe src=http://xxxx width=250 height=250></iframe>ui

    <?PHP /** * @blog http://www.phpddt.com * @param $string * @param $low 安全別級低 */
    function clean_xss(&$string, $low = False) { if (! is_array ( $string )) { $string = trim ( $string ); $string = strip_tags ( $string ); $string = htmlspecialchars ( $string ); if ($low) { return True; } $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string ); $no = '/%0[0-8bcef]/'; $string = preg_replace ( $no, '', $string ); $no = '/%1[0-9a-f]/'; $string = preg_replace ( $no, '', $string ); $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; $string = preg_replace ( $no, '', $string ); return True; } $keys = array_keys ( $string ); foreach ( $keys as $key ) { clean_xss ( $string [$key] ); } } //just a test
    $str = 'phpddt.com<meta http-equiv="refresh" content="0;">'; clean_xss($str); //若是你把這個註釋掉,你就知道xss攻擊的厲害了
    echo $str; ?>
相關文章
相關標籤/搜索