php 安全過濾函數範例程式碼

來源:互聯網
上載者:User
php 安全過濾函數代碼,防止使用者惡意輸入內容。

代碼如下:

//安全過濾輸入[jb] function check_str($string, $isurl = false) { $string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string); $string = str_replace(array("\0","%00","\r"),'',$string); empty($isurl) && $string = preg_replace("/&(?!(#[0-9]+|[a-z]+);)/si",'&',$string); $string = str_replace(array("%3C",'<'),'<',$string); $string = str_replace(array("%3E",'>'),'>',$string); $string = str_replace(array('"',"'","\t",' '),array('“','‘',' ',' '),$string); return trim($string); }

下面是為大家整理的一些過濾函數:

代碼如下:

/*** 安全過濾類-過濾javascript,css,iframes,object等不安全參數 過濾層級高*  Controller中使用方法:$this->controller->fliter_script($value)* @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標籤*  Controller中使用方法:$this->controller->fliter_html($value)* @param  string $value 需要過濾的值* @return string*/function fliter_html($value) {if (function_exists('htmlspecialchars')) return htmlspecialchars($value);return str_replace(array("&", '"', "'", "<", ">"), array("&", "\"", "'", "<", ">"), $value);}/*** 安全過濾類-對進入的資料加底線 防止SQL注入*  Controller中使用方法:$this->controller->fliter_sql($value)* @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);}/*** 安全過濾類-通用資料過濾*  Controller中使用方法:$this->controller->fliter_escape($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;}/*** 安全過濾類-字串過濾 過濾特殊有危害字元*  Controller中使用方法:$this->controller->fliter_str($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;}/*** 私人路勁安全轉化*  Controller中使用方法:$this->controller->filter_dir($fileName)* @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;}/*** 過濾目錄*  Controller中使用方法:$this->controller->filter_path($path)* @param string $path* @return array*/public function filter_path($path) {$path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);return rtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');}/*** 過濾PHP標籤*  Controller中使用方法:$this->controller->filter_phptag($string)* @param string $string* @return string*/public function filter_phptag($string) {return str_replace(array(''), array('<?', '?>'), $string);}/*** 安全過濾類-返回函數*  Controller中使用方法:$this->controller->str_out($value)* @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); //底線}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.