php获取前端提交数据类:支持危险数据过滤-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
php获取前端提交数据类:支持危险数据过滤
  1. 代码:
    /**
    * @desc:获取前端提交的数据,支持数据过滤
    * @author [Lee] <[]>
    */
    class getrequest{
    /*
     @desc:内部函数:过滤危险数据
     */
    private function safetydata($data){
        foreach($data as $k=>$v){
            if(is_array($v)){
                $data[$k] = $this->safetydata($v);
            }else{
                $tmp = trim($v);
                $tmp = addslashes($tmp);
                $data[$k] = $tmp;
            }
        }
        return $data;
    }
    /*
     @desc:判断前端传入方式,转换成能用数据
     */
    public function getrequestdata(){
        $data;
        $ret;
        $contenttype = strtolower($_SERVER['CONTENT_TYPE']);
        $method = strtolower($_SERVER['REQUEST_METHOD']);
        if($contenttype == 'application/json'){
            $data = file_get_contents('php://input');
            $data = json_decode($data,true);
        }elseif(in_array($contenttype,array('application/x-www-form-urlencoded','multipart/form-data')) || $method == 'post'){
            $data = $_POST;
        }elseif(in_array($contenttype,array('application/x-www-form-urlencoded','multipart/form-data')) || $method == 'get'){
            $data = $_GET;
        }else{
            parse_str(file_get_contents('php://input'),$data);
        }
        $ret = $this->safetydata($data);
        return $ret;
    }
    }
  2. 用法:
    $getrequest = new getrequest();
    $data = $getrequest->getrequestdata();
    var_dump($data);
  3. 测试
    php获取前端提交数据类:支持危险数据过滤
    php获取前端提交数据类:支持危险数据过滤

分享标题:php获取前端提交数据类:支持危险数据过滤
文章出自:http://scgulin.cn/article/iphcci.html