這篇文章主要介紹了CI架構安全過濾函數,結合執行個體形式分析了CodeIgniter架構去空、防止XSS的函數定義與使用方法,並附帶了原生PHP進行各種常見安全過濾相關操作技巧,需要的朋友可以參考下
本文執行個體講述了CI架構安全過濾函數。分享給大家供大家參考,具體如下:
1、CI架構版本:
/*** 自動過濾變數,進行XSS,去空* 支援:單個字串,多維陣列,數字* @param type $param = '常規字串' 或 array('字串1','字串2');* @return string|array*/final protected function html_trim($param=''){ if(is_array($param) && !empty ($param)){ return ($param); } if(is_string($param)){ return htmlspecialchars(trim($param)); } if(is_numeric($param)) { return (int)$param; } return $param;}
使用方式為:
$this->html_trim($this->input->post('refer_url',TRUE));
由於第二個參數為TRUE,預設已經進行了xss過濾
2、原生PHP版本
/** * 安全過濾類-過濾javascript,css,iframes,object等不安全參數 過濾層級高 * @param string $value 需要過濾的值 * @return string */function fliter_script($value) { $value = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i","&111n\\2",$value); $value = preg_replace("/(.*?)<\/script>/si","",$value); $value = preg_replace("/(.*?)<\/iframe>/si","",$value); $value = preg_replace ("//iesU", '', $value); return $value;}/** * 安全過濾類-過濾HTML標籤 * @param string $value 需要過濾的值 * @return string */function fliter_html($value) { if (function_exists('htmlspecialchars')) return htmlspecialchars($value); return str_replace(array("&", '"', "'", "<", ">"), array("&", "\"", "'", "<", ">"), $value);}/** * 安全過濾類-對進入的資料加底線 防止SQL注入 * @param string $value 需要過濾的值 * @return string */function fliter_sql($value) { $sql = array("select", 'insert', "update", "delete", "\'", "\/\*", "\.\.\/", "\.\/", "union", "into", "load_file", "outfile"); $sql_re = array("","","","","","","","","","","",""); return str_replace($sql, $sql_re, $value);}/** * 安全過濾類-通用資料過濾 * @param string $value 需要過濾的變數 * @return string|array */function fliter_escape($value) { if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = self::fliter_str($v); } } else { $value = self::fliter_str($value); } return $value;}/** * 安全過濾類-字串過濾 過濾特殊有危害字元 * @param string $value 需要過濾的值 * @return string */function fliter_str($value) { $badstr = array("\0", "%00", "\r", '&', ' ', '"', "'", "<", ">", " ", "%3C", "%3E"); $newstr = array('', '', '', '&', ' ', '"', ''', "<", ">", " ", "<", ">"); $value = str_replace($badstr, $newstr, $value); $value = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $value); return $value;}/** * 私人路勁安全轉化 * @param string $fileName * @return string */ function filter_dir($fileName) { $tmpname = strtolower($fileName); $temp = array(':/',"\0", ".."); if (str_replace($temp, '', $tmpname) !== $tmpname) { return false; } return $fileName;}/** * 過濾目錄 * @param string $path * @return array */public function filter_path($path) { $path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path); return rtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');}/** * 過濾PHP標籤 * @param string $string * @return string */public function filter_phptag($string) { return str_replace(array(''), array('<?', '?>'), $string);}/** * 安全過濾類-返回函數 * @param string $value 需要過濾的值 * @return string */public function str_out($value) { $badstr = array("<", ">", "%3C", "%3E"); $newstr = array("<", ">", "<", ">"); $value = str_replace($newstr, $badstr, $value); return stripslashes($value); //底線}
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!