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-mid-sorting and searching-162. Find Peak Element
mycode 54.81% class Solution(object): def findPeakElement(self, nums): """ :type num ...
- 图解http协议学习笔记
一 ,基本概念 1互联网相关的各协议族为tcp/ip协议(网际协议),tcp/ip ftp,DNS(通过域名解析ip地址),http(超文本传输协议) 还有很多协议 ,只是列举比较熟悉的 2tcp/ ...
- Oracle.DataAccess.Client.OracleCommand”的类型初始值设定项引发异常
Oracle.ManagedDataAccess.dll 连接Oracle数据库不需要安装客户端 最开始,连接Oracle 数据是需要安装客户端的,ado.net 后来由于微软未来不再支持 Syste ...
- java基础笔记1--关于线程死锁
关于线程死锁 什么是死锁: 在编写多线程的时候,必须要注意资源的使用问题,如果两个或多个线程分别拥有不同的资源, 而同时又需要对方释放资源才能继续运行时,就会发生死锁. 简单来说:死锁就是当一个或多个 ...
- 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】
json package com.taotao.common.utils; import java.util.List; import com.fasterxml.jackson.core.JsonP ...
- debian ssh/sftp
检查是否安装了openssh dpkg --get-selections | grep openssh 安装命令 sudo apt-get install openssh-server 安装成功的字样 ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_06.mybatis的环境搭建
创建实体类,实现Serializable接口 属性名和数据库的字段名保持一致 Date字段需要导入包 生成gettter和setter,再生成一个ToString的方法 创建持久层Dao 创建接口 里 ...
- 把自己活成AI
干啥都失败,所以从0重新开始. 把自己活成AI 准则1:一件事的对错,只代表这件事本身.多一点的解释都是错误. 准则2:大多数人默认遵守的就是规则和法律.大多数人默认承认的就是道德. 然后取其和法律的 ...
- laravel 使用PhantomMagick导出pdf ,在Linux下安装字体
git项目地址:https://github.com/anam-hossain/phantommagick sudo apt-get -y install fontconfig xfonts-util ...
- 正则表达式——推荐使用 Unicode 编码
常见的正则表达式的文档都是关于英文(ASCII字符)的,英文开发者通常也只需要处理ASCII字符,不需要处理中文这类多字符的字符.不过,依照李处ASCII字符的方式处理中文字符,就有可能出错. ...