溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php獲取前端提交數(shù)據(jù)類:支持危險(xiǎn)數(shù)據(jù)過濾

發(fā)布時間:2020-07-13 18:26:54 來源:網(wǎng)絡(luò) 閱讀:764 作者:Lee_吉 欄目:web開發(fā)
  1. 代碼:
    /**
    * @desc:獲取前端提交的數(shù)據(jù),支持?jǐn)?shù)據(jù)過濾
    * @author [Lee] <[<complet@163.com>]>
    */
    class getrequest{
    /*
     @desc:內(nèi)部函數(shù):過濾危險(xiǎn)數(shù)據(jù)
     */
    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:判斷前端傳入方式,轉(zhuǎn)換成能用數(shù)據(jù)
     */
    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獲取前端提交數(shù)據(jù)類:支持危險(xiǎn)數(shù)據(jù)過濾
    php獲取前端提交數(shù)據(jù)類:支持危險(xiǎn)數(shù)據(jù)過濾
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI