Skip to main content

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

Comments

Popular posts from this blog

6 guaranteed steps how to create CRON JOB FUNTION in wordpress

Create Cron Job function in Wordpress plugin Step 1: Register function on plugin activate  register_activation_hook(__FILE__, 'activate_one'); Step 2: add_filter function for interval //Filter for Adding multiple intervals add_filter( 'cron_schedules', 'intervals_schedule' ); // define interval function   function intervals_schedule($schedules) {   $schedules['everyminute'] = array(    'interval' => 60, // Every 1 minutes    'display'  => __( 'Every 1 minutes' )   );   return $schedules;  } Step 3: Activate hook function   //Schedule a first action if it's not already scheduled   function activate_one() {   if (!wp_next_scheduled('wpyog_cron_action')) {    wp_schedule_event( time(), 'everyminute', 'wpyog_cron_action');   }  } Step 4: Cron hook function   //Hook into that action that'll fire after 1 minutes   add_action('wpyog_cron_action', 'execute_

How to Create a jQuery Autocomplete in Wordpress

How to Create a jquery-ui Autocomplete in wordpress. Autocomplete provides suggestions while you type into the text field. In Wordpress we fetch dynamically matched pattern. Include javascript and css files in header. Create action inside functions.php or inside plugin code. add_action('wp_head', 'custom_register_scripts'); function custom_register_scripts(){ wp_register_style( 'techsudhir_jquery_ui_css', plugin_dir_url(__FILE__) . 'css/jquery-ui.css', false,'1.0.0' ); wp_enqueue_style( 'techsudhir_jquery_ui_css' ); wp_register_script('techsudhir_jquery_ui_js',plugin_dir_url(__FILE__) . 'js/jquery-ui.js',array('jquery'),'1.1', false); wp_enqueue_script('techsudhir_jquery_ui_js'); wp_localize_script( 'techsudhir_autocomplete', 'jqueryAutocomplete', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script( 'techsudhir_

Facebook Login

Step 1: Go on Apps & create new Apps Step 2: Enter the app name & choose category Step 3: Enter the site domain Script code <div id="fb-root"></div> <script   src="http://connect.facebook.net/en_US/all.js#appId=<?php echo '603609636351597';?>"> </script> <script>    FB.init({              appId:'<?php echo '603609636351597';?>', cookie:true,              status:true, xfbml:true           });   FB.getLoginStatus(function(response) {   }); Login function call on click function faceLogin(){  FB.login(function(response) {    if (response.authResponse) {  FB.api('/me', function(response) { $('input[id=facebook_login_fb]').val(response['id']); $('#fb_login_form').submit();  });    } else {  console.log('User cancelled login or did