繁体   English   中英

Cakephp Google RE Captcha不发布数据

[英]Cakephp Google RE Captcha not posting data

当根据其用户文档https://developers.google.com/recaptcha/docs/verify尝试将googles reCaptcha与'g-recaptcha-response'一起使用时,出现了未定义的变量错误

但是,当我提交用户表单时-Recaptcha中没有发布任何数据-这使我相信这是视图错误。

控制器功能

if ($this->request->is('post'))
        {
            //if ($this->Recaptcha->verify()) {

                //verify captcha
                debug($this->request->data);
                $recaptcha_secret = "xxxxxxxxxxxxxxxxxxxxxxx";
                $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$this->request->data['g-recaptcha-response']);
                $response = json_decode($response, true);
                if($response["success"] == true){
                }
                else{
                }
            }
        }

View.ctp

<script src='https://www.google.com/recaptcha/api.js'></script>
<?php
$this->set('title_for_layout', 'Sign Up '); ?>
    <!--    -- TESTACL1
                USERS ADD       --      -->

<div class="container-fluid">
    <div class="row">
        <!-- div class="col-md-3">
        </div -->
        <div class="col-md-1">
        </br></br>
        </div>        
        <div class="col-md-5">
            <div class="usersfor">
            <?php
                echo $this->Form->create(array(
                    'novalidate' => true,
                    'User'
                )); ?>

                <fieldset>
                    <legend><?php echo __('Sign Up for an Account'); ?></legend>
                <?php
                if ($this->Session->read('Auth.User')) {
                    echo 'It looks like you already have a Junto Account! We\'re pleased to hear it!';
                    echo '<br/>';
                    echo '<br/>*';
                    echo '<br/>**';
                    echo '<br/>***';
                    echo '<br/>****';
                    echo '<br/>*****';
                    echo '<br/>******';
                    echo '<br/>*******';
                    echo '<br/>******';
                    echo '<br/>****';
                    echo '<br/>***';
                    echo '<br/>**';
                    echo '<br/>*';
                }
                else {
                // echo $this->Form->input('group_id', array ('class'=>'form-control','label' => 'Type Of Account:', 'type'=>'text','value'=>'Registered', 'maxLength' => '20'));
                echo $this->Form->input('username', array(
                    'class' => 'form-control',
                    'label' => 'Email Address:',
                    'type' => 'text',
                    'maxLength' => '50',
                    'placeholder' => 'EG. johndoe@example.com',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->hidden('PassFrom', array(
                    'value' => ''
                ));
                echo $this->Form->input('password', array(
                    'placeholder' => '(8 - 20 characters)',
                    'div' => 'chckbx',
                    'class' => 'form-control',
                    'label' => 'Create Password:',
                    'type' => 'password',
                    'maxLength' => '20',
                    'style' => 'width:55%;',
                    'min' => 8
                ));
                echo $this->Form->input('first_name', array(
                    'placeholder' => 'EG. John',
                    'class' => 'form-control',
                    'label' => 'First Name:',
                    'type' => 'text',
                    'maxLength' => '45',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('last_name', array(
                    'placeholder' => 'EG. Doe',
                    'div' => 'chckbx',
                    'class' => 'form-control',
                    'label' => 'Last Name:',
                    'type' => 'text',
                    'maxLength' => '45',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('addressLineOne', array(
                    'placeholder' => 'EG. 99 Example Street',
                    'class' => 'form-control',
                    'label' => 'Address:',
                    'type' => 'text',
                    'maxLength' => '40',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('addressLineTwo', array(
                    'placeholder' => '(optional)',
                    'class' => 'form-control',
                    'label' => false,
                    'type' => 'text',
                    'maxLength' => '40',
                    'style' => 'width:75%;'
                ));
            ?>
            </div>
        </div>
        <div class="col-md-6">
        <p></br></p>
        <p></br></p>
        <p></br></p>
        <?php
        echo $this->Form->input('city', array(
            'placeholder' => 'EG. Melbourne',
            'class' => 'form-control',
            'label' => 'Suburb:',
            'type' => 'text',
            'maxLength' => '60',
            'style' => 'width:35%;'
        )); ?>

        <div class="row">
            <div class="col-md-6">
                <?php
                    echo $this->Form->input('state', array(
                    'class' => 'form-control',
                    'label' => 'State:',
                    'options' => array(
                        'VIC' => 'VIC',
                        'ACT' => 'ACT',
                        'NSW' => 'NSW',
                        'NT' => 'NT',
                        'QLD' => 'QLD',
                        'SA' => 'SA',
                        'TAS' => 'TAS',
                        'WA' => 'WA'
                    )
            )); ?>
            </div>
            <div class="col-md-6">
                <?php
                echo $this->Form->input('postcode', array(
                'placeholder' => '',
                'class' => 'form-control',
                'label' => 'Postcode:',
                'type' => 'text',
                'maxLength' => '4',
                'style' => 'width:40%;'
            )); ?>
            </div>
        </div>
        <br/>
        <?php
        echo $this->Form->input('telephoneNumber', array(
            'placeholder' => 'EG. 0401555777',
            'class' => 'form-control',
            'label' => 'Telephone Number (Include area code e.g. 0423232112):',
            'maxLength' => '10',
            'style' => 'width:37%;'
        )); ?>


        <b><?php echo "Date of Birth:"; ?></b>
        <?php
            echo $this->Form->input('dob', array(
            'div' => 'chckbx',
            'label' => '',
            'dateFormat' => 'DMY',
            'maxYear' => date('Y') ,
            'minYear' => date('Y') - 70
        ));
        // echo $this->Form->input('gender', array ('class'=>'form-control','label' => 'Gender:', 'options' => array('M'=>'Male','F'=>'Female')));
        // echo $this->Form->input('newsletterSubscription', array ('class'=>'form-control','label' => 'Subscribe to Newsletter?:', 'options' => array('Y'=>'Yes','N'=>'No')));
         ?>
        <br/>
        <?php
        $options = array(
            'm' => '&nbsp;&nbsp;Male&nbsp;&nbsp;&nbsp;',
            'f' => '&nbsp;&nbsp;Female'
        );
        $attributes = array(
            'legend' => false
        );
        echo $this->Form->radio('gender', $options, $attributes);
        echo $this->Form->input('newsletterSubscription', array(
            'div' => 'chckbx',
            'value' => 'y',
            'type' => 'checkbox',
            'hiddenField' => 'n',
            'label' => '&nbsp;&nbsp;Subscribe To Junto Club Newsletter?'
        ));
        }
        ?>
    <div class="g-recaptcha" data-sitekey="6LfGCQETAAAAAOEMaiCsSVxyypunTvGBSsFWr5XW"></div>
 </fieldset>
</br>

</br>
    <div class="submit" align="center">
    <?php
    echo $this->Form->end(__('Submit')); ?>
    <p></br></p>
    <p></br></p>
    </div>
    <!-- End -->
                <div class="col-md-2">
                </div>
            </div>
</div>
<div class="actions">
    <!-- <h3><?php /* echo __('Actions'); ?></h3>
<ul>

<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); */ ?> </li>
    </ul> -->
</div>

</div>

更新::该问题似乎是由div标签引起的。 通过将recaptcha小部件放在特定的div中,我能够解决此问题。

<script src='https://www.google.com/recaptcha/api.js'></script>
<?php
$this->set('title_for_layout', 'Sign Up '); ?>
    <!--    -- TESTACL1
                USERS ADD       --      -->

<div class="container-fluid">
    <div class="row">
        <!-- div class="col-md-3">
        </div -->
        <div class="col-md-1">
        </br></br>
        </div>        
        <div class="col-md-5">
            <div class="usersfor">
            <?php
                echo $this->Form->create(array(
                    'novalidate' => true,
                    'User'
                )); ?>
                <fieldset>
                    <legend><?php echo __('Sign Up for an Account'); ?></legend>
                <?php
                if ($this->Session->read('Auth.User')) {
                    echo 'It looks like you already have a Junto Account! We\'re pleased to hear it!';
                    echo '<br/>';
                    echo '<br/>*';
                    echo '<br/>**';
                    echo '<br/>***';
                    echo '<br/>****';
                    echo '<br/>*****';
                    echo '<br/>******';
                    echo '<br/>*******';
                    echo '<br/>******';
                    echo '<br/>****';
                    echo '<br/>***';
                    echo '<br/>**';
                    echo '<br/>*';
                }
                else {
                // echo $this->Form->input('group_id', array ('class'=>'form-control','label' => 'Type Of Account:', 'type'=>'text','value'=>'Registered', 'maxLength' => '20'));
                echo $this->Form->input('username', array(
                    'class' => 'form-control',
                    'label' => 'Email Address:',
                    'type' => 'text',
                    'maxLength' => '50',
                    'placeholder' => 'EG. johndoe@example.com',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->hidden('PassFrom', array(
                    'value' => ''
                ));
                echo $this->Form->input('password', array(
                    'placeholder' => '(8 - 20 characters)',
                    'div' => 'chckbx',
                    'class' => 'form-control',
                    'label' => 'Create Password:',
                    'type' => 'password',
                    'maxLength' => '20',
                    'style' => 'width:55%;',
                    'min' => 8
                ));
                echo $this->Form->input('first_name', array(
                    'placeholder' => 'EG. John',
                    'class' => 'form-control',
                    'label' => 'First Name:',
                    'type' => 'text',
                    'maxLength' => '45',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('last_name', array(
                    'placeholder' => 'EG. Doe',
                    'div' => 'chckbx',
                    'class' => 'form-control',
                    'label' => 'Last Name:',
                    'type' => 'text',
                    'maxLength' => '45',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('addressLineOne', array(
                    'placeholder' => 'EG. 99 Example Street',
                    'class' => 'form-control',
                    'label' => 'Address:',
                    'type' => 'text',
                    'maxLength' => '40',
                    'style' => 'width:75%;'
                ));
                echo $this->Form->input('addressLineTwo', array(
                    'placeholder' => '(optional)',
                    'class' => 'form-control',
                    'label' => false,
                    'type' => 'text',
                    'maxLength' => '40',
                    'style' => 'width:75%;'
                ));
            ?>

            </div>
            **<div class="g-recaptcha" data-sitekey="6LfGCQETAAAAAOEMaiCsSVxyypunTvGBSsFWr5XW"></div>**
        </div>
        <div class="col-md-6">
        <p></br></p>
        <p></br></p>
        <p></br></p>
        <?php
        echo $this->Form->input('city', array(
            'placeholder' => 'EG. Melbourne',
            'class' => 'form-control',
            'label' => 'Suburb:',
            'type' => 'text',
            'maxLength' => '60',
            'style' => 'width:35%;'
        )); ?>

        <div class="row">
            <div class="col-md-6">
                <?php
                    echo $this->Form->input('state', array(
                    'class' => 'form-control',
                    'label' => 'State:',
                    'options' => array(
                        'VIC' => 'VIC',
                        'ACT' => 'ACT',
                        'NSW' => 'NSW',
                        'NT' => 'NT',
                        'QLD' => 'QLD',
                        'SA' => 'SA',
                        'TAS' => 'TAS',
                        'WA' => 'WA'
                    )
            )); ?>
            </div>
            <div class="col-md-6">
                <?php
                echo $this->Form->input('postcode', array(
                'placeholder' => '',
                'class' => 'form-control',
                'label' => 'Postcode:',
                'type' => 'text',
                'maxLength' => '4',
                'style' => 'width:40%;'
            )); ?>
            </div>
        </div>
        <br/>
        <?php
        echo $this->Form->input('telephoneNumber', array(
            'placeholder' => 'EG. 0401555777',
            'class' => 'form-control',
            'label' => 'Telephone Number (Include area code e.g. 0423232112):',
            'maxLength' => '10',
            'style' => 'width:37%;'
        )); ?>


        <b><?php echo "Date of Birth:"; ?></b>
        <?php
            echo $this->Form->input('dob', array(
            'div' => 'chckbx',
            'label' => '',
            'dateFormat' => 'DMY',
            'maxYear' => date('Y') ,
            'minYear' => date('Y') - 70
        ));
        // echo $this->Form->input('gender', array ('class'=>'form-control','label' => 'Gender:', 'options' => array('M'=>'Male','F'=>'Female')));
        // echo $this->Form->input('newsletterSubscription', array ('class'=>'form-control','label' => 'Subscribe to Newsletter?:', 'options' => array('Y'=>'Yes','N'=>'No')));
         ?>
        <br/>
        <?php
        $options = array(
            'm' => '&nbsp;&nbsp;Male&nbsp;&nbsp;&nbsp;',
            'f' => '&nbsp;&nbsp;Female'
        );
        $attributes = array(
            'legend' => false
        );
        echo $this->Form->radio('gender', $options, $attributes);
        echo $this->Form->input('newsletterSubscription', array(
            'div' => 'chckbx',
            'value' => 'y',
            'type' => 'checkbox',
            'hiddenField' => 'n',
            'label' => '&nbsp;&nbsp;Subscribe To Junto Club Newsletter?'
        ));
        }
        ?>

        <div class="submit" align="center">
        <?php echo $this->Form->end(__('Submit')); ?>
        </div>
 </fieldset>
</br>

</br>

    <!-- End -->
                <div class="col-md-2">
                </div>
            </div>
</div>
<div class="actions">
    <!-- <h3><?php /* echo __('Actions'); ?></h3>
<ul>

<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); */ ?> </li>
    </ul> -->
</div>

</div>

但是,为什么这会以这种方式影响我呢?

引发错误

Notice (8): Undefined index: g-recaptcha-response [APP\Controller\UsersController.php, line 73]
Code Context
UsersController::add() - APP\Controller\UsersController.php, line 73
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE\Cake\Controller\Controller.php, line 490
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 191
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 165
[main] - APP\webroot\index.php, line 108

调试

array(
    'User' => array(
        'password' => '*****',
        'username' => '',
        'PassFrom' => '',
        'first_name' => '',
        'last_name' => '',
        'addressLineOne' => '',
        'addressLineTwo' => '',
        'city' => '',
        'state' => 'VIC',
        'postcode' => '',
        'telephoneNumber' => '',
        'dob' => array(
            'day' => '27',
            'month' => '01',
            'year' => '2015'
        ),
        'gender' => '',
        'newsletterSubscription' => 'n'
    )
)

我有同样的问题。 g-recaptcha-response在POST数组中,但不在$ this-> data中。 看到:

Array
(
    [_method] => POST
    [data] => Array
        (
            [BlogPostComment] => Array
                (
                    [name] => shannon
                    [email] => shannon@home.com
                    [text] => asdf
                    [blog_post_id] => 11
                    [remote_ip] => 184.00.00.001
                )

        )

    [g-recaptcha-response] => 03AHJ_VutSTvzfJze9pkzpcwQE3Xdf [ ... truncated...]

这是因为其他表单元素具有ID,例如BlogPostCommentName或BlogPostCommentEmail,它们以如下形式进入$ this-> data数组:

[BlogPostComment] => Array
       (
           [name] => shannon
           [email] => shannon@home.com
       )

...等等。 但是Recaptcha输入的ID是g-recaptcha-response,因此不被视为该集合的一部分。 这使得难以验证响应。 我尚不知道如何解决该问题,但是无论如何,这就是您发布的数据可能即将结束的地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM