How to use Google reCaptcha with Cakephp 3.x

To use google reCaptcha with php you can read How to use google recaptcha with PHP . 
In this tutorial we learn how to use google recaptcha with cakepphp 3.x we need two keys that provide by google .you can get your recaptcha keys from google developer account.

۱٫first we need to call google recaptcha js so open your projectFolder/src/template/layout/default.ctp or you can call it into your header.ctp

<?= $this->Html->script(‘https://www.google.com/recaptcha/api.js’) ?>

۲٫ Now we have to save keys into our project that we can access from anywhere so copy this code and paste it into YourProject/Config/bootstrap.php

Configure::write(‘google_recatpcha_settings’, array(
‘site_key’=>’SITEKEY’,
‘secret_key’=>’SECRETKEY’
));

۳٫Now open your appConteoller (projectFolder/src/Controller/AppController)

Add these code into initialize function of appController

public function initialize()
{
$usecaptcha = 1;
$this->set(‘usecaptcha’,$usecaptcha); //we can on or off captcha on website by passing  ۰ or 1
}

AND ALSO ADD A NEW FUNCTION
public function verifyRecatpcha($aData)
{
if(!$aData)
{
return true;
}
if(isset($aData[‘g-recaptcha-response’]))
{
$recaptcha_secret = Configure::read(‘google_recatpcha_settings.secret_key’);
$url = “https://www.google.com/recaptcha/api/siteverify?secret=”.$recaptcha_secret.”&response=”.$aData[‘g-recaptcha-response’];
$response = json_decode(@file_get_contents($url));

if($response->success == true)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
۴٫ Now create a controller(src/Controller/UsersController.php) and index.ctp(src/Template/Users/index.ctp) file of it. and create a form in index.ctp file and paste this code where you want to add google recaptcha

<?php if($usecaptcha==1) { ?>
<div class=”form-group”>
<div class=”g-recaptcha” data-sitekey=”<?php echo $publickeycaptcha; ?>”></div>
</div>
<?php } ?>

After adding these lines your register form will look like

<div class=”col-sm-12 col-md-12 minheight”>
<div class=”col-sm-3 col-md-3″></div>
<div class=”col-sm-6 col-md-6 content-box-large”>
<div class=”users form”>
<?= $this->Flash->render() ?>

<?= $this->Form->create() ?>
<?= $this->element(‘error’) ?>
<fieldset>
<legend><?= __(‘Register’) ?></legend>

<div class=”form-group”>
<?= $this->Form->input(‘username’, array(‘type’ => ‘text’,’class’ => ‘form-control’,’label’ => ‘Username’,’required’=>true)); ?>
</div>
<div class=”form-group”>
<?= $this->Form->input(’email’, array(‘type’ => ‘text’,’class’ => ‘form-control’,’label’ => ‘Email’,’required’=>true)); ?>
</div>
<div class=”form-group”>
<?= $this->Form->input(‘password’, array(‘type’ => ‘password’,’class’ => ‘form-control’,’label’ => ‘Password’,’required’=>true)); ?>
</div>
<div class=”form-group”>
<?= $this->Form->input(‘confirm_password’, array(‘type’ => ‘password’,’class’ => ‘form-control’,’label’ => ‘Confirm Password’,’required’=>true)); ?>
</div>
<div class=”col-sm-12 paddingleftnone”>
<div class=”col-sm-1 paddingleftnone paddingrightnone”>
<?= $this->Form->input(‘terms’, [‘type’ => ‘checkbox’,’class’ => ”,’label’=>”]); ?>
</div>
<div class=”col-sm-11 aligncenter paddingtopten” style=”padding-top:13px;”>I agree to the <a href=”#”>Terms of Use.</a>  </div>
</div>

</fieldset>
<?php if($usecaptcha==1) { ?>
<div class=”form-group”>
<div class=”g-recaptcha” data-sitekey=”<?php echo $publickeycaptcha; ?>”></div>
</div>
<?php } ?>
<div class=”form-group”>
<?= $this->Form->submit(‘Submit’, array(‘div’ => false,’class’ => ‘btn btn-success’, ‘title’ => ‘Create Account’)); ?>
</div>

<?= $this->Form->end() ?>
</div>
</div>

</div>

۵٫Now we have to check recaptcha that its valid or not in controller so open your UsersController and paste this code

public function register()
{

if ($this->request->is(‘post’)) {

if(usecaptcha==1)
{
if(!$this->verifyRecatpcha($this->request->data))
{

$this->Flash->error(__(‘Invalid Captcha, try again’),array(‘class’ => ‘alert alert-danger’));
return  $this->redirect(array(“controller” => “Users”,”action” => “register”));
}
}
}
}

So thats all for how to use google reCaptcha with cakephp 3.x

1 ماه پیش

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

This site uses Akismet to reduce spam. Learn how your comment data is processed.