I am getting a undefined variable error when trying to use googles reCaptcha with 'g-recaptcha-response' according to their user docs https://developers.google.com/recaptcha/docs/verify
However when i submit my user form - no data is posted from the recaptcha - this makes me believe it is a view error.
Controller funtion
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' => ' Male ',
'f' => ' 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' => ' 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>
update:: The issue appears to be caused by div tags. I was able to resolve the issue by placing the recaptcha widget inside a particular 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' => ' Male ',
'f' => ' 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' => ' 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>
But why would this be affecting me in this way?
Error thrown
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
Debug
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'
)
)
I'm having the same problem. g-recaptcha-response is in the POST array, but it's not in $this->data. See:
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...]
)
That's because the other form elements have ids like BlogPostCommentName or BlogPostCommentEmail, which get into $this->data array as:
[BlogPostComment] => Array
(
[name] => shannon
[email] => shannon@home.com
)
...and so on. But the recaptcha input's id is g-recaptcha-response, so it's not considered part of that set. This makes it difficult to validate the response. I don't know how to solve that yet, but anyway, that's where your posted data is probably ending up.