温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php获取前端提交数据类:支持危险数据过滤

发布时间:2020-07-13 18:26:54 来源:网络 阅读:811 作者:Lee_吉 栏目:web开发
  1. 代码:
    /** * @desc:获取前端提交的数据,支持数据过滤 * @author [Lee] <[<complet@163.com>]> */ 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获取前端提交数据类:支持危险数据过滤
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI