thinkphp6 输入变量过滤

thinkphp6 输入变量过滤

官方文档地址:php

https://www.kancloud.cn/manual/thinkphp6_0/1037519#_100html

变量过滤

框架默认没有设置任何全局过滤规则,你能够在app\Request对象中设置filter全局过滤属性:thinkphp

namespace app;

class Request extends \think\Request
{
    protected $filter = ['htmlspecialchars'];
}

也支持使用Request对象进行全局变量的获取过滤,过滤方式包括函数、方法过滤,以及PHP内置的Types of filters,咱们能够设置全局变量过滤方法,支持设置多个过滤方法,例如:json

Request::filter(['strip_tags','htmlspecialchars']),

也能够在获取变量的时候添加过滤方法,例如:app

Request::get('name','','htmlspecialchars'); // 获取get变量 并用htmlspecialchars函数过滤
Request::param('username','','strip_tags'); // 获取param变量 并用strip_tags函数过滤
Request::post('name','','org\Filter::safeHtml'); // 获取post变量 并用org\Filter类的safeHtml方法过滤

能够支持传入多个过滤规则,例如:框架

Request::param('username','','strip_tags,strtolower'); // 获取param变量 并依次调用strip_tags、strtolower函数过滤

若是当前不须要进行任何过滤的话,能够使用函数

// 获取get变量 而且不进行任何过滤 即便设置了全局过滤
Request::get('name', '', null);

对于body中提交的json对象,你无需使用php://input去获取,能够直接当作表单提交的数据使用,由于系统已经自动处理过了post

支持 php 内置的字符串函数:spa

https://www.php.net/manual/zh/ref.strings.php.net

相关文章
相关标签/搜索