php - Yii2 在隐身模式下无法验证您的数据提交

标签 php yii2 yii2-advanced-app

我使用以下方法将后端和前端分开:

后端配置/main.php

$config = [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
            'cookieValidationKey' => 'sdsdsdsd-e8Fhoa1PdHzzfB2VTON9Nfh',
            'class' => 'common\components\Request',
            'web'=> '/backend/web',
            'adminUrl' => '/cpanel'
        ],
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
        'user' => [
            'identityClass' => 'common\models\AdminUser',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-project-backend', /*'httpOnly' => true*/],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'project-backend',
            'timeout' => 60*60*24*30,
        ],
    ],
    'params' => $params,
];

前端配置/main.php

$config =  [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
            'cookieValidationKey' => 'wmWhVSIv-e8Fhoa1PdHzzfB2VTON9Nfh',            
            'class' => 'common\components\Request',
            'web' => '/frontend/web'
        ],
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-project-frontend', /*'httpOnly' => true*/],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'project-frontend',
            'timeout' => 60*60*24*30,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],        
    ],
    'params' => $params,
    //'defaultRoute' => 'site/index'
];

现在它可以在正常浏览器模式下完美运行。但是当我尝试使用隐身模式登录时,第一次尝试时会出现以下错误:

Unable to verify your data submission

之后,如果我重新加载页面并尝试再次登录,它会正常工作。

我的表单是使用 ActiveForm 生成的,因此 CSRF token 在登录页面中可用。

那么如何解决这个问题呢?

最佳答案

你可以在特定的 Controller / Action 中指定验证 false

包含 Yii 类

use Yii;

在 Action 中

Yii::$app->controller->enableCsrfValidation = false;

或者在 Controller 内部

$this->enableCsrfValidation = false;

关于php - Yii2 在隐身模式下无法验证您的数据提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41583355/

相关文章:

mysql - 选择查询 Yii2 中的左连接 - 'single' 中的未知列 'on clause'

yii2 - 如何从 Yii2 basic 禁用 Bootstrap ?

javascript - Yii2 禁用字段

yii2 - 有条件地显示 Nav 小部件的项目

php - 在mysql中查找关系

php - 如何在 WooCommerce 中使用 add_to_cart() 方法?

php - Yii2 - 如果 View 文件不存在则重定向

php - yii2 advanced 中的公共(public)资源放在哪里?

php - 使用 Sequel PRO 或 phpMyAdmin 在 MySQL 中导入 XML

javascript - Yii2 创建一个没有模型的表单