您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

启用Csrf后POST数据时出现的400错误_PHP

2025/9/19 14:05:06发布21次查看
最近一直出现这样的错误,一直在查找原因,偶然看到一篇解决的文章,分享给大家看看。
第一种解决办法是关闭csrf
public function init(){ $this->enablecsrfvalidation = false;}
第二种解决办法是在form表单中加入隐藏域
csrftoken ?>>
第三种解决办法是在ajax中加入_csrf字段
var csrftoken = $('meta[name=csrf-token]').attr(content);$.ajax({ type: 'post', url: url, data: {_csrf:csrftoken}, success: success, datatype: datatype});
yii这个匹配的过程和yii::$app->request->csrftoken 这个值存储位置说明:
存储位置
protected function createcsrfcookie($token) { $options = $this->csrfcookie; $options['name'] = $this->csrfparam; $options['value'] = $token; return new cookie($options); }
校验方法
public function validatecsrftoken($token = null) { $method = $this->getmethod(); // only validate csrf token on non-safe methods http://www.w3.org/protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 if (!$this->enablecsrfvalidation || in_array($method, ['get', 'head', 'options'], true)) { return true; } $truetoken = $this->loadcsrftoken(); if ($token !== null) { return $this->validatecsrftokeninternal($token, $truetoken); } else { return $this->validatecsrftokeninternal($this->getbodyparam($this->csrfparam), $truetoken) || $this->validatecsrftokeninternal($this->getcsrftokenfromheader(), $truetoken); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product