Example of CI Framework Security Filter Function

  • 2021-10-24 19:02:30
  • OfStack

In this paper, an example is given to describe the security filtering function of CI framework. Share it for your reference, as follows:

1. CI Framework Version:


/**
*  Automatically filter variables and perform XSS Go empty 
*  Support: Single string, multidimensional array, number 
* @param type $param = ' General string '  Or  array(' String 1',' String 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;
}

Used as follows:


$this->html_trim($this->input->post('refer_url',TRUE));

Since the second parameter is TRUE, xss filtering has been performed by default

2. Native PHP version


/**
 *  Security filter class - Filter javascript,css,iframes,object Equal unsafe parameters   High filtering level 
 * @param string $value  Values to be filtered 
 * @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;
}
/**
 *  Security filter class - Filter HTML Label 
 * @param string $value  Values to be filtered 
 * @return string
 */
function fliter_html($value) {
     if (function_exists('htmlspecialchars')) return htmlspecialchars($value);
     return str_replace(array("&", '"', "'", "<", ">"), array("&", "\"", "'", "<", ">"), $value);
}
/**
 *  Security filter class - Underline the incoming data   Prevent SQL Injection 
 * @param string $value  Values to be filtered 
 * @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);
}
/**
 *  Security filter class - Universal data filtering 
 * @param string $value  Variables to be filtered 
 * @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;
}
/**
 *  Security filter class - String filtering   Filter special harmful characters 
 * @param string $value  Values to be filtered 
 * @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;
}
/**
 *  Safe transformation of private road strength 
 * @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;
}
/**
 *  Filter catalog 
 * @param string $path
 * @return array
 */
public function filter_path($path) {
     $path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);
     return rtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');
}
/**
 *  Filter PHP Label 
 * @param string $string
 * @return string
 */
public function filter_phptag($string) {
     return str_replace(array(''), array('<?', '?>'), $string);
}
/**
 *  Security filter class - Return function 
 * @param string $value  Values to be filtered 
 * @return string
 */
public function str_out($value) {
     $badstr = array("<", ">", "%3C", "%3E");
     $newstr = array("<", ">", "<", ">");
     $value = str_replace($newstr, $badstr, $value);
     return stripslashes($value); // Underline 
}

More readers interested in CodeIgniter can check the topics of this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "ThinkPHP Common Methods Summary", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to the PHP programming based on CodeIgniter framework.


Related articles: