Monday, 12 May 2014

How To Add Google Captcha in Cakephp




Step 1: First you should do is sign up and register you domain for private and public key.



Step 2: Then download reCAPTCHA PHP library and put recaptchalib.php file on recaptchalib folder on verdors folder like vendors.

Step 3: Put this file in bootstrap.php
 pp/config/bootstrap.php
//--------------for google captcha start here------------//
 Configure::write('recatpch_settings', array(
   'public_key'=>'6Lf-nu8SAAAAAIGBhxpcJGxqI03LSP1vcNtjYGrN ',
'private_key'=>'6Lf-nu8SAAAAAC7tXfYaZi0GDKvD5aa7JO7CNXkB '
 ));
//-----------end here-----------------------------------//


Step 4: Add this to your ctp

Now you have to add script where you have want to add captcha in ctp

<?php echo $this->Html->script('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js');?>

<script>
$(function(){
    Recaptcha.create("<?php echo Configure::read("recatpch_settings.public_key")?>", 'recaptcha_div', {
    theme: "red",
    callback: Recaptcha.focus_response_field});
});
</script>

Step 5: Add this in ctp


<?php echo $this->Form->create('ContactMsg', array('type'=>"file",'inputDefaults' => array('label' => false,'div' => false)));?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="114" >Full Name :*</td>
        <td width="284" ><?php echo $this->Form->input('name');?></td>
    </tr>
    <tr>
        <td width="114" >Email :*</td>
        <td width="284" ><?php echo $this->Form->input('email');?></td>
    </tr>
    <tr valign="top">
        <td width="114" > </td>
        <td width="284">
            <div id="recaptcha_div"></div>
        </td>
    </tr>
    <tr valign="top">
        <td width="114" > </td>
        <td width="284">
            <?php echo $this->Form->submit('Submit')?>
        </td>
    </tr>
</table>

<?php echo $this->Form->end();?>

Step 6: Add this in controller function

function register()//example
{
App::import('Vendor', 'recaptchalib',array('file' => 'recaptchalib/recaptchalib.php'));   
if (!$resp->is_valid)
{
$this->Session->setFlash('The reCAPTCHA wasn\'t entered correctly. Please, try again.','error');
else 
{
$this->User->create();
if($this->User->save($data))
{
$id = $this->User->getLastInsertId();
$active = '<a href="'.HTTP_ROOT.'homes/activation_link/'.base64_encode(convert_uuencode($id)).'" target="_blank">Click here to confirm your account</a>'; 
$replace = array('{name}','{email}','{username}','{active}','{password}');
$with = array($data['User']['fname'].' '.$data['User']['lname'],$data['User']['email'],$data['User']['username'], $active,$pass);
$this->send_email($replace,$with,'new_registration',NO_REPLY,$data['User']['username'],$data['User']['email']);
$this->Session->setFlash('Registration Successfull. Please check Link in your email to activate your account.','success');
$this->redirect(array('controller'=>'homes','action'=>'index'));
}
}

}

<?php echo $this->Form->create('ContactMsg', array('type'=>"file",'inputDefaults' => array('label' => false,'div' => false)));?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="114" >Full Name :*</td>
        <td width="284" ><?php echo $this->Form->input('name');?></td>
    </tr>
    <tr>
        <td width="114" >Email :*</td>
        <td width="284" ><?php echo $this->Form->input('email');?></td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <div id="recaptcha_div"></div>
        </td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <?php echo $this->Form->submit('Submit')?>
        </td>
    </tr>
</table>
<?php echo $this->Form->end();?>
- See more at: http://blog.jambura.com/2013/01/02/use-recaptcha-in-cakephp-app/#sthash.axJCzSCL.dpuf
Now we are going to build simple from to enter name and email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php echo $this->Form->create('ContactMsg', array('type'=>"file",'inputDefaults' => array('label' => false,'div' => false)));?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="114" >Full Name :*</td>
        <td width="284" ><?php echo $this->Form->input('name');?></td>
    </tr>
    <tr>
        <td width="114" >Email :*</td>
        <td width="284" ><?php echo $this->Form->input('email');?></td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <div id="recaptcha_div"></div>
        </td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <?php echo $this->Form->submit('Submit')?>
        </td>
    </tr>
</table>
<?php echo $this->Form->end();?>
- See more at: http://blog.jambura.com/2013/01/02/use-recaptcha-in-cakephp-app/#sthash.axJCzSCL.dpuf
Now we are going to build simple from to enter name and email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php echo $this->Form->create('ContactMsg', array('type'=>"file",'inputDefaults' => array('label' => false,'div' => false)));?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="114" >Full Name :*</td>
        <td width="284" ><?php echo $this->Form->input('name');?></td>
    </tr>
    <tr>
        <td width="114" >Email :*</td>
        <td width="284" ><?php echo $this->Form->input('email');?></td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <div id="recaptcha_div"></div>
        </td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <?php echo $this->Form->submit('Submit')?>
        </td>
    </tr>
</table>
<?php echo $this->Form->end();?>
- See more at: http://blog.jambura.com/2013/01/02/use-recaptcha-in-cakephp-app/#sthash.axJCzSCL.dpuf

0 comments:

Post a Comment