本文实例讲述了CI框架安全过滤函数。分享给大家供大家参考,具体如下:
1、CI框架版本:
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版本
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("/(.*?)/si","",$value);
$value = preg_replace("/(.*?)/si","",$value);
$value = preg_replace ("//iesU", '', $value);
return $value;
}
function fliter_html($value) {
if (function_exists('htmlspecialchars')) return htmlspecialchars($value);
return str_replace(array("&", '"', "'", "<", ">"), array("&", """, "'", "<", ">"), $value);
}
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);
}
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;
}
function fliter_str($value) {
$badstr = array(" ", "%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;
}
function filter_dir($fileName) {
$tmpname = strtolower($fileName);
$temp = array(':/'," ", "..");
if (str_replace($temp, '', $tmpname) !== $tmpname) {
return false;
}
return $fileName;
}
public function filter_path($path) {
$path = str_replace(array("'",'#','=','`','$','%','&',';'), '', $path);
return rtrim(preg_replace('/(/){2,}|(\){1,}/', '/', $path), '/');
}
public function filter_phptag($string) {
return str_replace(array(''), array('', '?>'), $string);
}
public function str_out($value) {
$badstr = array("<", ">", "%3C", "%3E");
$newstr = array("<", ">", "<", ">");
$value = str_replace($newstr, $badstr, $value);
return stripslashes($value); //下划线
}
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend frameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。



