无法验证 CSRF 令牌


CSRF token could not be verified

我知道这已经问过了,但我的问题有点不同。在我的.php文件中,我有:

<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name;
?>
<div id="page-content">
    <h1 class="gasparregular">Welcome back, <?php echo Yii::app()->user->table->name ?>!</h1>
    <h2 style="color:white;">Current Promotion: <?php echo Yii::app()->user->table->promo ?></h2>
    <span class="help-text">To check, please enter their xxxID:</span>
    <?php
    if(isset($_POST['submit'])) 
    {
        $message=
        'First Name: '.$_POST['xxxid'].'<br />
        Last Name: '.$_POST['xxxid'].'<br />
        Phone:  '.$_POST['xxxid'].'<br />
        Email:  '.$_POST['xxxid'].'<br />
        Comments: '.$_POST['xxxid'].'
        ';
        require "phpmailer/class.phpmailer.php";
        // Instantiate Class  
        $mail = new PHPMailer();  
        // Set up SMTP  
        $mail->IsSMTP();               
        $mail->SMTPAuth = true;         
        $mail->SMTPSecure = "ssl"; 
        $mail->Host = "smtp.gmail.com"; 
        $mail->Port = 465; 
        $mail->Encoding = '7bit';
        // Authentication  
        $mail->Username   = "xxx@xxx.com";
        $mail->Password   = "dswdcssd1yfs";
        // Compose
        $mail->SetFrom("xxx@xxx.com");
        $mail->AddReplyTo("xxx@xxx.com");
        $mail->Subject = "Hello";
        $mail->MsgHTML($message);
        // Send To  
        $mail->AddAddress("ggggdssae@hotmail.com", "Recipient Name");
        $result = $mail->Send();
        $message = $result ? 'Successfully Sent!' : 'Sending Failed!';     
        unset($mail);
        $URL="google.ca"; 
        header ("Location: $URL"); 
    }
    ?>
<form name="form1" id="form1" action="" method="post">
    <fieldset>
        <input type="text" name="xxxid" placeholder="xxx ID" />
        <br />
        <input type="submit" name="submit" value="Validate" />
    </fieldset>
</form>

当我输入一个数字并单击验证按钮时,它会发送电子邮件。在没有 Yii 的情况下,它在我当地的环境中工作正常。但是当我把它放在它使用 Yii 的实时站点上时,我得到:

错误无法验证 CSRF 令牌。

表单需要处理 CSRF,这里有很多选项:

您可以从控制器禁用它:

$this->enableCsrfValidation = false;

或者在视图中使用 ActiveForm,它将处理 CSRF。

更新:

在Stack Overflow中有很多关于这个问题的问题,但重要的是要知道CSRF(跨站点请求伪造)是为了确保提交的表单数据来自您的应用程序。

更多信息: http://www.yiiframework.com/doc-2.0/yii-web-request.html