Php Yii错误400尝试删除帖子时无法验证CSRF令牌

Php Yii错误400尝试删除帖子时无法验证CSRF令牌,php,yii,Php,Yii,当我试图删除一篇帖子时,出现了以下错误: Yii Error 400 The CSRF token could not be verified 我不知道到底是什么导致了这一切,也不知道这与什么有关。以下是我的删除操作: public function actionDelete($id) { if (Yii::app()->request->isPostRequest) { // we only allow deletion

当我试图删除一篇帖子时,出现了以下错误:

Yii Error 400 The CSRF token could not be verified
我不知道到底是什么导致了这一切,也不知道这与什么有关。以下是我的删除操作:

    public function actionDelete($id) {

         if (Yii::app()->request->isPostRequest) {
                // we only allow deletion via POST request
                $this->loadModel($id)->delete();

                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
              throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    protected function afterDelete()
    {
        parent::afterDelete();
        Image::model()->deleteAll('name='.$this->id);
        Date::model()->deleteAll('tbl_show_id='.$this->id);
        Press::model()->deleteAll('tbl_show_id='.$this->id);
    }

看来你已经启用了。如果您想使用它,请阅读文档,并确保在每个POST请求中发送CSRF令牌。

CSRF将不断向您提供此错误,因为您正在使用URL(GET)进行删除

为了使用CSRF验证,您应该使用生成CSRF令牌并在每次发布时提交的有效表单提出请求

查找:
Yii CForm

我也遇到了同样的问题,但下面解决了它。希望能有帮助。 我补充说

以下代码:

<?php
    echo CHtml::linkButton('Delete',array(
        'submit'=>$this->createUrl('delete',array('id'=>$model->id)),
        'confirm'=>"Are you sure want to delete ".$item->product->name."from the shopping cart?",
        'params'=> array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)));
?>


谢谢。

即使您正确地遵循了Yii CSRF文档,您的错误也可能是由缓存系统引起的。在我的例子中,服务器缓存了登录页面,然后使用登录表单一次又一次地提供相同的令牌,从而在验证时返回false

<?php
    echo CHtml::linkButton('Delete',array(
        'submit'=>$this->createUrl('delete',array('id'=>$model->id)),
        'confirm'=>"Are you sure want to delete ".$item->product->name."from the shopping cart?",
        'params'=> array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)));
?>