Cakephp Google RE Captcha没有发布数据

时间:2015-01-27 06:02:31

标签: php cakephp recaptcha

我在尝试使用googles reCaptcha并使用' g-recaptcha-response'时收到一个未定义的变量错误根据用户文档https://developers.google.com/recaptcha/docs/verify

然而,当我提交我的用户表单时 - 没有从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'
    )
)

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 g-recaptcha-response在POST数组中,但它不在$ this-&gt;数据中。参见:

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...]

那是因为其他表单元素有像BlogPostCommentName或BlogPostCommentEmail这样的ID,它们进入$ this-&gt;数据数组:

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

......等等。但是recaptcha输入的id是g-recaptcha-response,所以它不被认为是该集合的一部分。这使得难以验证响应。我还不知道如何解决这个问题,但无论如何,这就是你发布的数据可能已经结束的地方。