在生产服务器上使用 CakePHP3 "'_Token' was not found in request data"



我有一个用CakePHP 3.6制作的网站,它似乎在我的计算机上运行良好(使用WAMP,PHP 5.6.31,Apache 2.4.27(,但是当放入生产服务器(Ubuntu 14.04,PHP 5.6.37,Apache 2.4.7(时具有不同的行为。

有一个 POST 请求仅在生产服务器上生成以下错误:

'_Token' was not found in request data.
CakeControllerExceptionAuthSecurityException

我启用了安全组件,但我不想禁用它。所有表单域都是使用 FormHelper 创建的。这些字段不会使用 Javascript 进行修改。

错误并不总是发生,主要取决于用户在表单中输入的内容。我无法确定哪种内容会产生此错误。发送的数据是一组可能包含任何内容的字符串。但我看不出内容与此错误有何关系。

下面是生成表单的代码示例

<?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>';
?>

多亏了格雷格,我才能找到问题所在。正如他所说,如果你发送太多数据,它可能会被截断。

为了诊断问题,我实际上显示了安全组件接收的数据。您可以在vendorcakephpcakephpsrcControllerComponentSecurityComponent.php.在此文件中有一个函数_validToken(Controller $controller)。显示$check变量的内容可能会有所帮助(我为此使用了pr()函数(。

我注意到我发送的一些数据实际上丢失了。显然,正如CakePHP所说,_Token也丢失了。

我唯一要做的就是增加 php 中的max_input_vars.ini

相关内容

最新更新