The so-called sensitive characters refer to articles that contain unhealthy or reactionary information, information that affects society, and will be regarded as sensitive characters. Because sometimes the sensitive characters entered by the user will invisibly affect the correct execution of the program, the famous database injection attack is to add database control instructions to the query conditions, thereby achieving the attacker's purpose.
This article mainly introduces the PHP form sensitive character filtering class and its usage examples. It analyzes in detail the filtering function for sensitive characters in form generation and submission. It is a very practical skill and is needed. Friends can refer to it.The specific analysis is as follows:
rn",$titarray[0],$outform,$name); } /** * 表单验证 及全部 ck_类函数 */ private function ck_split($standard,$name,$title,$find,$error) { // 非必填缺省跳过 if(eregi('N',$find) && emptyempty($this->post[$name]))return false; // 必填缺省检测 if(eregi('Y',$find) && emptyempty($this->post[$name]))return "["J{$name}","$error"],"; $t_error = null; // 多项检测 $arr = explode(',',$standard); // POST数据检测 if(!emptyempty($arr))foreach ($arr as $var) { if(trim($var)!='') { switch ($this->post) { case is_array($this->post[$name]): // 数组类的检测 foreach ($this->post[$name] as $_var) { $t_error.= ($this->ck_open($_var,trim($var)))?"":$error; if($t_error)break; } break; default: $t_error.= ($this->ck_open($this->post[$name],trim($var)))?"":$error; break; } if($t_error)break; } } return ($t_error)? "["J{$name}","$t_error"],":""; } // 函数调用 private function ck_open($string,$str) { $functi = $this->ck_detected($str); return ($this->$functi($string,$str))? true:false; } // 类型判断 private function ck_detected($str) { $detect = (eregi("^[a-zA-Z]*$",$str))? "{$str}Detect":'lengthDetect'; if(!in_array($detect,$this->array['class'])) { location('index.php',$ck,' Lack of function !!!'); } return $detect; } //-------------------------------------以下为检测函数可外部调用 // 长度 public function lengthDetect($string,$str){ $len = split('-',trim($str)); return (strlen($string) > ($len[0]-1) && strlen($string) < ($len[1]+1))? true:false; } // 价格 public function moneyDetect($str){ return preg_match("/^(-|+)?d+(.d+)?$/",$str); } // 邮件 public function emailDetect($str){ return preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/", $str); } // 网址 public function urlDetect($str){ return preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>"])*$/", $str); } // 数字型 public function numDetect($str){ return is_numeric($str); } // 中文 public function cnDetect($str){ return preg_match("/^[x7f-xff]+$/", $str); } // 字母 public function enDetect($str){ return preg_match("/^[A-Za-z]+$/", $str); } // 数字字母混合 public function numenDetect($str){ return preg_match("/^([a-zA-Z0-9_-])+$/",$str); } // 电话号码 public function telDetect($str){ return ereg("^[+]?[0-9]+([xX-][0-9]+)*$", $str); } // 敏感词 public function keyDetect($str){ return (!preg_match("/$badkey/i",$str)); } //-----------------------------------------------------输出 // 字符替换 public function ck_filter($str){ $str=(is_array($str))? implode(",",$str):$str; $str=nl2br($str); //将回车替换为 $str=htmlspecialchars($str); //将特殊字元转成 HTML 格式。 //$str=str_replace(array(" ",' '),array(" ",'< ?'),$str); //替换空格替换为 return $str; } // 转义 function ck_escape($str) { if (!get_magic_quotes_gpc())return addslashes($str); return $str; } // MD5加密 public function ck_md5($str){ return MD5($str); } // base64加密 public function ck_base64($str){ return base64_encode($str); } // 时间 function ck_time($str){ // time_r() 来在公用函数文件 if(!is_numeric($str)) { return time_r($str); } else return $str; } // 有条件注销(数字) public function ck_cancel($str){ return (!is_numeric($str))? $str:""; } // 无条件注销 public function ck_delete(){ return null; } // js错误提示 private function jsError() { if(emptyempty($this->error))return false; return " "; } } // 演示: $form[1] =array( 'text'=>array('title','','产品名称','size=40','产品名称不可缺少!','Y','cn,1-30'), 'text1'=>array('categories','','产品名称','','','Y_base64'), 'select'=>array('superiors','||1|2|Y_3','产品类别|选择|1|2|3','','必选项','Y'), 'radio'=>array('superiors1','|1|Y_2|3','产品xun|产品1|产品2|产品3','','必选项','Y'), 'checkbox'=>array('superiors2',array(1=>'11',2=>'22',3=>'33'),'','','必选项','Y'), 'file'=>array('ddd','','文件'), ); $form =array ( 'login' => array ( 'text' => array ( 0 => 'user', 1 => '', 2 => '用户名', 3 => 'size=20', 4 => '!', 5 => 'Y', 6 => 'numen,6-12', ), 'password' => array ( 0 => 'pass', 1 => '', 2 => '密 码', 3 => 'size=22', 4 => '密码格式错误!', 5 => 'Y_md5', 6 => 'numen,6-12', ), 'radio' => array ( 0 => 'time', 1 => '|7200|3600|1800', 2 => 'cookies有效时间|2小时|1小时|30分钟', 3 => '', 4 => '', 5 => 'N_delete', 6 => '', ), ), ); // 表单提交效验 $past = $_form->postForm($form['login']); $dd = array('title'=>'标题','categories'=>'类别'); // $dd 为已有的信息(如更新时的信息输出) POST数据位内部处理具有优先权 if(!emptyempty($past)) { echo "
"; print_r($past); echo"
"; } echo '
';
Copy after login
The above is the detailed content of PHP filter sensitive character class example code. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn