【问题标题】:Unexpected field 'g-recaptcha-response' in POST data on CakePHP 3CakePHP 3 上的 POST 数据中出现意外字段“g-recaptcha-response”
【发布时间】:2018-02-01 21:22:40
【问题描述】:

我使用 CakePHP 3 创建了一个网站。我有一个静态页面,可以通过以下方式联系我们:

在contactus.ctp中:

<?=$this->Form->create(); ?>
        <?=$this->Form->hidden('form_type',['value' => 'contact']) ?>
        <?=$this->Form->input('name',[
            'label' => false,
            'placeholder' => 'Your Full Name',
            'required' => true
        ]); ?>

        <?=$this->Form->input('email',[
            'label' => false,
            'placeholder' => 'Your Email',
            'type' => 'email',
            'require' => true
        ]); ?>

        <?=$this->Form->textarea('message',[
            'placeholder' => 'Your message...'
        ]) ?>

        <?= $this->Recaptcha->display()?> 

        <button>Submit Query!</button>

        <?=$this->Form->end(); ?>

我使用以下链接创建了 Recaptcha:

https://github.com/agiletechvn/Recaptcha

就在提交按钮旁边,我有 Recaptcha。

在 pageController 中我进行了提交检查:

if($this->request->is(['post']) && $this->request->data('form_type') == 'contact'){

            $name = $this->request->data('name');
            $email = $this->request->data('email');
            $message = $this->request->data('message');

            if(!$name){
                $this->Flash->set('Please enter a name' . $name,['element' => 'error']);
            } elseif (!$email || filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
                $this->Flash->set('The email you entered is invalid',['element' => 'error']);
            } elseif(!$message){
                $this->Flash->set('Message cannot be blank',['element' => 'error']);
            } else {
                if($this->Recaptcha->verify()) {
                $emailObj = new Email();
                $emailObj
                    ->from(['contactus@mydomain.com' => 'Developer'])
                    ->replyTo([$email => $name])
                    ->to(['contactus@contactus.com'])
                    ->template('pages/contactus')
                    ->viewVars([
                        'quickAction' => [
                            'description' => 'Contact form',
                            'action' => "from: $name"
                        ],
                        'name' => 'contactus@mydomain.com',
                        'senderName' => $name,
                        'email' => $email,
                        'message' => $message
                    ])
                    ->subject('Contact email from ' . $name)
                    ->send();

                $this->Flash->set('Your message has been sent', ['element' => 'success']);
            }

            $this->Flash->error(__('Please pass Google Recaptcha first'));
          }

如果我点击提交按钮,我会得到:

Unexpected field 'g-recaptcha-response' in POST data 

我将 reCaptcha 代码移到了表单之外。一切正常,但验证码却位于这样的随机位置之外:

我该如何解决这个问题?

【问题讨论】:

    标签: cakephp cakephp-3.0 recaptcha cakephp-3.4


    【解决方案1】:

    如果您正在使用 CakePHP 安全组件,并且该组件无法识别您的表单字段之一,则此消息可能会显示。您应该使用以下方法解锁此字段:

    $this->Form->unlockField('g-recaptcha-response');
    

    更多信息:CakePHP 3.x Security Component

    【讨论】:

    • 感谢您的快速回复。你必须把这个放在哪里?对不起,我对 cake php 完全陌生
    • 把它放在.ctp文件中,在$this-&gt;Form-&gt;end()之前。
    猜你喜欢
    • 2015-08-21
    • 1970-01-01
    • 2019-01-19
    • 2015-10-05
    • 1970-01-01
    • 2015-02-25
    • 2017-12-24
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多