一、请求类型

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的更多相关文章

  1. ThinkPHP5.0中Request请求对象的使用和常用的操作

    request的使用 第一种方法 在控制器头部添加request引用 然后在方法里调用 ‘instance’类 然后在调用方法: public function index($name='name') ...

  2. thinkphp5.0.19 表单令牌

    助手函数token() [F:\phpStudy\WWW\csweb\thinkphp\helper.php] request类token()方法 [F:\phpStudy\WWW\csweb\thi ...

  3. ThinkPHP5.0中Request请求对象的常用操作

    获取当前系统参数 // 获取当前域名 echo '获取当前域名:'.$request->domain() . '<br/>'; // 获取当前入口文件 echo '获取当前入口文件: ...

  4. thinkphp5.0学习笔记(三)获取信息,变量,绑定参数

    1.构造函数: 控制器类必须继承了\think\Controller类,才能使用: 方法_initialize 代码: <?php namespace app\lian\controller; ...

  5. 对thinkphp5.0框架的实例学习

    不论是渗透测试还是代码审计的过程中会碰到用不同的框架搭建起来的网站,熟悉这些框架的基本原理,会帮助我们快速的理解漏洞原理,提高干活效率,所以自己本地搭了个php环境,来入门实例学习下thinkphp5 ...

  6. ThinkPHP5.0.21&5.1.* 代码执行和命令执行漏洞利用

    ThinkPHP5.0.21&5.1.* 代码执行和命令执行漏洞利用 ThinkPHP5.0.21&5.1.*  exploit code execution and command ...

  7. thinkphp5.0和thinkphp3.2的区别不同之处

    先看目录结构: thinkphp 5.0的目录结构, 文档:https://www.kancloud.cn/manual/thinkphp5/118008 project 应用部署目录 ├─appli ...

  8. thinkphp5.0 输入变量

    可以通过Request对象完成全局输入变量的检测.获取和安全过滤,支持包括$_GET.$_POST.$_REQUEST.$_SERVER.$_SESSION.$_COOKIE.$_ENV等系统变量,以 ...

  9. ThinkPHP5 核心类 Request 远程代码漏洞分析

    ThinkPHP5 核心类 Request 远程代码漏洞分析 先说下xdebug+phpstorm审计环境搭建: php.ini添加如下配置,在phpinfo页面验证是否添加成功. [XDebug] ...

随机推荐

  1. Mybaits配置多个数据库操作sql环境

    mybitas可以配置sql语句适用于不同数据库下的操作,因为不同数据库sql语句可能有差别,接下来介绍如果进行操作 1.在jdbc.properprites配置驱动 jdbc.driver=com. ...

  2. GitHub - 解决 GitHub Page 404

    带有下划线的文件报 404 解决:在仓库文件夹根目录添加.nojekyll文件 参见: Bypassing Jekyll on GitHub Pages - The GitHub Blog How t ...

  3. spring的组件工厂后置处理器——BeanFactoryPostProcessor

    作用和调用时机 spring有两种后置处理器: 1. 组件后置处理器——org.springframework.beans.factory.config.BeanPostProcessor: 2. 工 ...

  4. 阶段3 1.Mybatis_09.Mybatis的多表操作_6 分析mybatis多对多的步骤并搭建环境

    示例:用户和角色             一个用户可以有多个角色             一个角色可以赋予多个用户         步骤:             1.建立两张表:用户表,角色表    ...

  5. 5.2.k8s.Secret

    #Secret Secret存储密码.token.密钥等敏感数据 Secret以Volume或环境变量方式使用 #Secret类型 Opaque : base64 编码格式的 Secret kuber ...

  6. 郝斌_GUI

    85事件处理 import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java ...

  7. zabbix添加主机后无法显示解决

    第一次添加主机后显示正常,后来删除了主机,重新添加了一下主机再也无法显示主机,很苦恼,原来需要点击重设,

  8. 【算法与数据结构】二叉堆和优先队列 Priority Queue

    优先队列的特点 普通队列遵守先进先出(FIFO)的规则,而优先队列虽然也叫队列,规则有所不同: 最大优先队列:优先级最高的元素先出队 最小优先队列:优先级最低的元素先出队 优先队列可以用下面几种数据结 ...

  9. is_selected()检查是否选中该元素

    is_selected()检查是否选中该元素,一般针对单选框,复选框,返回的结果是bool 值, 以百度登录页面为案例,来验证"下次自动登录"是否勾选,默认是勾选的,返回的结 果应 ...

  10. 【ABAP系列】SAP ABAP 运算符

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 运算符   前 ...