thinkphp5.0.19 request
一、请求类型
request类中 [F:\phpStudy\WWW\csweb\thinkphp\library\think\Request.php] 获取请求类型的方法分别是:
isGet() 、isPost()、isPut()、isDelete()、isHead()、isPatch()、isOptions()
/**
* 是否为GET请求
* @access public
* @return bool
*/
public function isGet()
{
return $this->method() == 'GET';
} /**
* 是否为POST请求
* @access public
* @return bool
*/
public function isPost()
{
return $this->method() == 'POST';
} /**
* 是否为PUT请求
* @access public
* @return bool
*/
public function isPut()
{
return $this->method() == 'PUT';
} /**
* 是否为DELTE请求
* @access public
* @return bool
*/
public function isDelete()
{
return $this->method() == 'DELETE';
} /**
* 是否为HEAD请求
* @access public
* @return bool
*/
public function isHead()
{
return $this->method() == 'HEAD';
} /**
* 是否为PATCH请求
* @access public
* @return bool
*/
public function isPatch()
{
return $this->method() == 'PATCH';
} /**
* 是否为OPTIONS请求
* @access public
* @return bool
*/
public function isOptions()
{
return $this->method() == 'OPTIONS';
}
它们都调用了method():
/**
* 当前的请求类型
* @access public
* @param bool $method true 获取原始请求类型
* @return string
*/
public function method($method = false)
{
if (true === $method) {
// 获取原始请求类型
return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
} elseif (!$this->method) {
if (isset($_POST[Config::get('var_method')])) {
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST);
} elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} else {
$this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
}
}
return $this->method;
}
method(true)是获取原始请求类型。直接读$_SERVER['REQUEST_METHOD']判断。
method()则是先判断 "伪装请求" 类型,其次才是真实的请求类型。
配置文件中的var_method、var_ajax、var_pjax则是伪装请求的配置变量:

下面表单将post伪装为put:
<form method="post" action="">
<input type="text" name="name" value="Hello">
<input type="hidden" name="_method" value="PUT" >
<input type="submit" value="提交">
</form>
public function method($method = false)
{
if (true === $method) {
// 获取原始请求类型
return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
} elseif (!$this->method) {
if (isset($_POST[Config::get('var_method')])) { // post类型表单请求
//获取伪装请求内容
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST); //$this->post($_POST)或$this->get($_POST)等处理$_POST数据
} elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { //request header HTTP_X_HTTP_METHOD_OVERRIDE中定义的表单请求类型
$this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} else { // get类型表单请求
$this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
}
}
return $this->method;
}
ajax、pjax的判断:
/**
* 当前是否Ajax请求
* @access public
* @param bool $ajax true 获取原始ajax请求
* @return bool
*/
public function isAjax($ajax = false)
{
//根据header中的HTTP_X_REQUESTED_WITH判断是否为xmlhttprequest
$value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
$result = ('xmlhttprequest' == $value) ? true : false;
if (true === $ajax) {
return $result;
} else {
//如果不是ajax请求则判断是否为模拟的ajax请求
return $this->param(Config::get('var_ajax')) ? true : $result;
}
} /**
* 当前是否Pjax请求
* @access public
* @param bool $pjax true 获取原始pjax请求
* @return bool
*/
public function isPjax($pjax = false)
{
$result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
if (true === $pjax) {
return $result;
} else {
return $this->param(Config::get('var_pjax')) ? true : $result;
}
}
二、设置或获取header:
/**
* 设置或者获取当前的Header
* @access public
* @param string|array $name header名称
* @param string $default 默认值
* @return string
*/
public function header($name = '', $default = null)
{
//$this->header为空则设置
if (empty($this->header)) {
$header = [];
// 使用apache_request_headers()函数获取请求头request header
if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
$header = $result;
}
//使用 $_SERVER获取请求头 request header
else {
$server = $this->server ?: $_SERVER;
//针对所有 $_SERVER['HTTP_*'] 元素
foreach ($server as $key => $val) {
if (0 === strpos($key, 'HTTP_')) {
//将HTTP_USER_AGENT形式转化为User-Agent形式(首字母大写)
$key = str_replace('_', '-', strtolower(substr($key, 5)));
$header[$key] = $val;
}
}
if (isset($server['CONTENT_TYPE'])) {
$header['content-type'] = $server['CONTENT_TYPE'];
}
if (isset($server['CONTENT_LENGTH'])) {
$header['content-length'] = $server['CONTENT_LENGTH'];
}
}
//转化为小写
$this->header = array_change_key_case($header);
}
if (is_array($name)) {
return $this->header = array_merge($this->header, $name);
}
//$name为空则返回所有header
if ('' === $name) {
return $this->header;
}
//将$name转为小写,并将其中的"_"替换为"-"
$name = str_replace('_', '-', strtolower($name));
//查找header并返回
return isset($this->header[$name]) ? $this->header[$name] : $default;
}
三、获取content-type:
public function contentType()
{
$contentType = $this->server('CONTENT_TYPE');
if ($contentType) {
if (strpos($contentType, ';')) {
//存在多个只会用第一个
list($type) = explode(';', $contentType);
} else {
$type = $contentType;
}
return trim($type);
}
return '';
}
四、获取请求体(php://input):
/**
* 获取当前请求的php://input
* @access public
* @return string
*/
public function getInput()
{
return $this->input;
}
$this->input在构造函数处初始化:
protected function __construct($options = [])
{
foreach ($options as $name => $item) {
if (property_exists($this, $name)) {
$this->$name = $item;
}
}
if (is_null($this->filter)) {
$this->filter = Config::get('default_filter');
} // 保存 php://input
//1、php://input是个只读流,用于获取请求体(request body)
//2、$_POST可获取application/x-www-form-urlencoded 和 multipart/form-data-encoded 两种Content Type的请求体。
//3、$_POST不能获取诸如 application/json 等Content Type的请求体数据,php://input可以。
//4、content type为multipart/form-data-encoded时,php://input无效。即上传文件时不能使用php://input获取数据,可用$_FILES。
//5、相比较于 $HTTP_RAW_POST_DATA , php://input 无需额外地在php.ini中 激活 always-populate-raw-post-data ,而且对于内存的压力也比较小。
//reference http://www.digpage.com/web_request.html#id8
$this->input = file_get_contents('php://input');
}
thinkphp5.0.19 request的更多相关文章
- ThinkPHP5.0中Request请求对象的使用和常用的操作
request的使用 第一种方法 在控制器头部添加request引用 然后在方法里调用 ‘instance’类 然后在调用方法: public function index($name='name') ...
- thinkphp5.0.19 表单令牌
助手函数token() [F:\phpStudy\WWW\csweb\thinkphp\helper.php] request类token()方法 [F:\phpStudy\WWW\csweb\thi ...
- ThinkPHP5.0中Request请求对象的常用操作
获取当前系统参数 // 获取当前域名 echo '获取当前域名:'.$request->domain() . '<br/>'; // 获取当前入口文件 echo '获取当前入口文件: ...
- thinkphp5.0学习笔记(三)获取信息,变量,绑定参数
1.构造函数: 控制器类必须继承了\think\Controller类,才能使用: 方法_initialize 代码: <?php namespace app\lian\controller; ...
- 对thinkphp5.0框架的实例学习
不论是渗透测试还是代码审计的过程中会碰到用不同的框架搭建起来的网站,熟悉这些框架的基本原理,会帮助我们快速的理解漏洞原理,提高干活效率,所以自己本地搭了个php环境,来入门实例学习下thinkphp5 ...
- ThinkPHP5.0.21&5.1.* 代码执行和命令执行漏洞利用
ThinkPHP5.0.21&5.1.* 代码执行和命令执行漏洞利用 ThinkPHP5.0.21&5.1.* exploit code execution and command ...
- thinkphp5.0和thinkphp3.2的区别不同之处
先看目录结构: thinkphp 5.0的目录结构, 文档:https://www.kancloud.cn/manual/thinkphp5/118008 project 应用部署目录 ├─appli ...
- thinkphp5.0 输入变量
可以通过Request对象完成全局输入变量的检测.获取和安全过滤,支持包括$_GET.$_POST.$_REQUEST.$_SERVER.$_SESSION.$_COOKIE.$_ENV等系统变量,以 ...
- ThinkPHP5 核心类 Request 远程代码漏洞分析
ThinkPHP5 核心类 Request 远程代码漏洞分析 先说下xdebug+phpstorm审计环境搭建: php.ini添加如下配置,在phpinfo页面验证是否添加成功. [XDebug] ...
随机推荐
- LeetCode_509.斐波那契数
LeetCode-cn_509 509.斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) ...
- rbac权限组件整合到实际项目的全过程详述
rbac简介 项目的GitHub地址 欢迎Download&Fork&Star:https://github.com/Wanghongw/CombineRbac 另外,本文只简单介绍一 ...
- WPF DevExpress Chart控件 需要引用的类库
DevExpress.Charts.v16.1.Core.dll DevExpress.Data.v16.1.dll DevExpress.Mvvm.v16.1.dll DevExpress.Xpf. ...
- Python Module_subprocess_子进程(程序调用)
目录 目录 前言 软件环境 认识subprocess Popen Constructor构造函数 Class Popen的参数 args 调用程序 调用Shell指令 stdinstdoutstder ...
- webpack中的 chunk,module,bundle的区别,以及hidden modules是什么
hidden modules是什么: chunk,module,bundle的区别 总结: module是指任意的文件模块,等价于commonjs中的模块 chunks是webpack处理过程中被分组 ...
- Microsoft.Office.Interop.Excel Find 操作
public void SearchLoactions(string filepath, int start, int end ,string expectvalue) { if (end >= ...
- 【Hibernate】---Query、Criteria、SQLQuery
一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-con ...
- JSP———数据交互【2】
内置对象application 实现用户之间的数据共享 与session 对象不同的是,所有客户的 application 对象是相同的一个,即所有的客户共享这个内置的 application 对象 ...
- 【MM系列】SAP 物料帐下修改物料的价格
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 物料帐下修改物料的价格 ...
- DB.JDBC_jar_下载
1.Download Microsoft JDBC Driver for SQL Server - SQL Server _ Microsoft Docs.html(https://docs.micr ...