douhua1760 2018-08-09 20:34
浏览 60
已采纳

“在生产服务器上使用CakePHP3在请求数据中找不到'_Token'

I have a website made with CakePHP 3.6 that seemed to work fine on my computer (using WAMP, PHP 5.6.31, Apache 2.4.27) but has a different behaviour when put in a production server (Ubuntu 14.04, PHP 5.6.37, Apache 2.4.7).

There is this one POST request that generates the following error ONLY on the production server :

'_Token' was not found in request data.

Cake\Controller\Exception\AuthSecurityException

I have the security component enabled, and I don't want to disable it. All form fields are created with the FormHelper. The fields are not modified with Javascript.

The error does not always happen and mostly depend on the content that is entered in the forms by the user. I could not identify what kind of content would generate this error. The data sent is a groupe of strings that might contain anything. But I don't see how the content is relevant for this error.

Here is a sample of the code that generates the form

<?php

    echo '<div class="custom-card">';
    echo $this->Form->create($keyword);
    echo '<table class="table table-striped table-bordered">';

    // Create Table Headers with languages name
    $tableHeaders = ["Mot clé"];
    for ($i = 0; $i < count($languagesEnabled); $i++)
    {
        array_push($tableHeaders, "{$languagesEnabled[$i]->name} (version {$languagesEnabled[$i]->version})");
    }
    array_push($tableHeaders, "");
    echo '<thead>';
    echo $this->Html->tableHeaders($tableHeaders);
    echo '</thead>';

    //Fill Table Cells with keywords and sentences
    for ($i = 0; $i < count($keywords); $i++)
    {
        $keywordName = $this->Form->text("keywords.$i.name", ["value" => $keywords[$i]->name, 'class' => 'form-control']);
        $keywordId = $this->Form->hidden("keywords.$i.id", ["value" => $keywords[$i]->id]);

        $tableCells = [$keywordName . " " . $keywordId];
        $tableCells = array_pad($tableCells, count($languages) - 1, "");
        for ($j = 0; $j < count($languagesEnabled); $j++)
        {
            $sentenceLanguageId = $this->Form->hidden("keywords.$i.sentences.$j.language_id", ["value" => $languagesEnabled[$j]->id]);
            $sentenceKeywordId = $this->Form->hidden("keywords.$i.sentences.$j.keyword_id", ["value" => $keywords[$i]->id]);
            $sentenceArray = ["value" => findSentenceValueInArray($keywords[$i]->sentences, $languagesEnabled[$j]->id)];
            $sentenceValue = $this->Form->textarea("keywords.$i.sentences.$j.sentence", [
                'value' => $sentenceArray,
                'class' => 'form-control',
                'id' => "area-$i-$j",
                'onfocus' => "autosize(document.getElementById('area-$i-$j'))"
                ]);
            $tableCells[$j + 1] = $sentenceValue . $sentenceLanguageId . $sentenceKeywordId;
        }
        $tableCells[count($languagesEnabled) + 1] = $this->Html->link('Supprimer',
            ['controller' => 'Keywords', 'action' => 'remove', $keywords[$i]->id],
            ['confirm' => 'Êtes vous sûr de vouloir supprimer la phrase ?']);
        echo $this->Html->tableCells($tableCells);
    }
    echo "</table>";
    echo $this->Form->submit('Valider', ['class'=>'btn btn-primary']);
    echo $this->Form->end();
    echo '</div>';
?>
  • 写回答

1条回答 默认 最新

  • douyongwan5946 2018-08-14 19:28
    关注

    Thanks to Greg I was able to find what the problem was. As he stated, if you send too much data it can be truncated.

    To diagnose the problem I actually displayed the data received by the SecurityComponent. You can access it in vendor\cakephp\cakephp\src\Controller\Component\SecurityComponent.php. In this file there is a function _validToken(Controller $controller). Displaying the content of the $check variable might help (I used the pr() function for this).

    I noticed that some data I sent was actually missing. And obviously, as stated by CakePHP, the _Token was also missing.

    The only thing I had to do was increase max_input_vars in my php.ini

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度