网站地图    收藏   

主页 > php专栏 > php类库 >

php 表单敏感字符过滤代码 - php类库

来源:自学PHP网    时间:2014-11-30 12:53 作者: 阅读:

[导读] /***表单生成验证文件*/$_form=newformHtmlFind();classformHtmlFind{/***输出表单函数*$formKey表单键*$infoArray更新时的原始信息数组*/p......

php 表单敏感字符过滤代码

  1. /** 
  2. * 表单生成验证文件 
  3. */ 
  4. $_form = new formHtmlFind(); 
  5. class formHtmlFind{ 
  6.         /** 
  7.          * 输出表单函数 
  8.          * $formKey  表单键 
  9.          * $infoArray 更新时的原始信息数组 
  10.          */ 
  11.  
  12.         public function formHtml($array,$infoArray=''
  13.         { 
  14.                 // 检测数组是否存在 
  15.                 if(emptyempty($array))return false; 
  16.                 $newform = null; 
  17.                 // 信息数组(更新信息) 
  18.                 $this->infoArray = !emptyempty($infoArray)?$infoArray:array(); 
  19.                 $this->array['class'] =  get_class_methods(get_class()); 
  20.                 foreach ($array as $key =>$arr
  21.                 { 
  22.                         // 键值转换为纯英文 
  23.                         $key = preg_replace("/[^a-z]/i",'',$key); 
  24.                         // 生成表单 
  25.                         $newform .= $this->outputForm($arr,$key); 
  26.                 } 
  27.                 // 输出表单 
  28.                 return $newform.$this->jsError(); 
  29.         } 
  30.         /** 
  31.          * 生成表单函数 
  32.          */ 
  33.         private function outputForm($arr,$key
  34.         { 
  35.                 $value = null; 
  36.                 if(emptyempty($arr))return false; 
  37.                 // input Type 
  38.                 $type   = $key
  39.                 // input NAME 
  40.                 $name   = trim($arr[0]); 
  41.                 // input 初始值 不包含多选,单选类 
  42.                 $value  = (!emptyempty($this->infoArray[$name]))? trim($this->infoArray[$name]):trim($arr[1]); 
  43.                 $value  = emptyempty($this->post[$name])? $value :trim($this->post[$name]); 
  44.                 // input Title 
  45.                 $title  = trim($arr[2]); 
  46.                 // 样式 
  47.                 $style  = trim($arr[3]); 
  48.                 if($key!=="hidden"
  49.                 { 
  50.                         $dt = "<dt>{$title}</dt><dd>"
  51.                         // js错误提示 
  52.                         $dd = "<tt id="J{$name}"></tt></dd>rn"
  53.                 } 
  54.                 return (!preg_match("/checkbox|select|radio/i",$key))? 
  55.                 $dt.$this->newInput($type,$name,$value,$style,$title).$dd
  56.                 $this->formSelect($type,$name,$arr[1],$title,$style); // 多选类 
  57.         } 
  58.         /** 
  59.          * 提交数据检测 
  60.          */ 
  61.         public function postForm($array
  62.         { 
  63.                 // 检测数组是否存在 
  64.                 if(emptyempty($array)||emptyempty($_POST))return false; 
  65.                 $this->post           =  $_POST
  66.                 $this->array['class'] =  get_class_methods(get_class()); 
  67.                 foreach ($array as $key =>$arr
  68.                 { 
  69.                         // 键值转换为纯英文 
  70.                         $key = preg_replace("/[^a-z]/i",'',$key); 
  71.                         // 检测 注销file类表单 
  72.                         if (!emptyempty($arr)&&'file' != $key)$newData[trim($arr[0])] = $this->postFind($arr,$key); 
  73.                 } 
  74.                 // 输出表单 
  75.                 if(!emptyempty($this->error)) 
  76.                 { 
  77.                         return false; 
  78.                 } 
  79.                 else return $newData
  80.         } 
  81.         /** 
  82.          * 生成表单 
  83.          */ 
  84.         private function newInput($type,$name,$value,$style,$title
  85.         { 
  86.                 switch ($type
  87.                 { 
  88.                         case 'text'
  89.                                 // 单行文本 
  90.                                 return  "<input type="text" name="{$name}" value="{$value}" {$style}/>"
  91.                                 break
  92.                         case 'password'
  93.                                 //密码输入 
  94.                                 return "<input type="password" name="{$name}" {$style}/>"
  95.                                 break
  96.                         case ''
  97.                                 //多行文本 
  98.                                 return "<textarea name="{$name}" {$style}/>{$value}</textarea>"
  99.                                 break
  100.                         case 'hidden'
  101.                                 // 隐藏 
  102.                                 return "<input type="hidden" name="{$name}" value="{$value}" {$style}/>"
  103.                                 break
  104.                         case 'file'
  105.                                 // 文件上传 
  106.                                 return "<input type= "file"name="{$name}" {$style}/>"
  107.                                 break
  108.                         case 'submit'
  109.                                 // 提交 
  110.                                 return "<input type="submit" name="{$name}" value="$value" $style}/>"
  111.                                 break
  112.                         default
  113.                                 return "{$type}类型错误!!!"
  114.                                 break
  115.                 } 
  116.         } 
  117.         /** 
  118.          * 提交信息检测 
  119.          * 错误返回error 
  120.          */ 
  121.         private function postFind($arr,$key
  122.         { 
  123.                 if(emptyempty($arr))return false; 
  124.                 $name = $title =$error =$find =$standard =null; 
  125.                 // input NAME 
  126.                 $name     = trim($arr[0]); 
  127.                 // input Title 
  128.                 $title    = trim($arr[2]); 
  129.                 // 错误提示 
  130.                 $error    = trim($arr[4]); 
  131.                 // 检测类型 Y N 
  132.                 $find     = trim($arr[5]); 
  133.                 // 检测标准 
  134.                 $standard = trim($arr[6]); 
  135.                 // 
  136.                 if(!emptyempty($standard))$this->error .=$this->ck_split($standard,$name,$title,$find,$error); 
  137.                 // 转换为字符串 
  138.                 if(is_array($this->post[$name]))$this->post[$name] = implode(",",$this->post[$name]); 
  139.                 // 转义或其他转化 
  140.                 $KKarray = array(); 
  141.                 if(preg_match("/Y|N/is",$find)) 
  142.                 { 
  143.                         $KKarray       = split("_"$find); 
  144.                         // 转义或过滤 
  145.                         $escape_filter = (!emptyempty($KKarray[1]))?'ck_'.$KKarray[1]:''
  146.                         // 输出通过检测的合法数据 
  147.                         $data          = ($escape_filter)?$this->$escape_filter($this->post[$name]):$this->post[$name]; 
  148.  
  149.                 } 
  150.                 else  $data        = ""
  151.                 // 输出新的数据 
  152.                 return $data
  153.         } 
  154.         /** 
  155.          * 多选类表单生成 
  156.          */ 
  157.         private function formSelect($type,$name,$value,$title,$style
  158.         { 
  159.                 $outform = null; 
  160.                 // 触发更新和提交动作时的初始 
  161.            

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论