Skip to main content

How to use google recaptcha with PHP


How to use google recaptcha with PHP

step1

first sign up here(google recaptcha api).
The key pair consists of a site key and secret.you can download recaptcha file from google recaptch php library 

step2

now  you will need to insert this code inside the <form> element where the reCAPTCHA widget will be placed:

  require_once('recaptchalib.php');
  $publickey = "your_public_key"; // you got this from the signup page
  echo recaptcha_get_html($publickey);

Example-

  <html>
    <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
      <!-- your HTML content -->

      <form method="post" action="verify.php">
        <?php
          require_once('recaptchalib.php');
          $publickey = "your_public_key"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
        <input type="submit" />
      </form>

      <!-- more of your HTML content -->
    </body>
  </html>
 
 
Step 3 
NOw you can check response by this code
<?php  

//first i retrieved the recaptcha coming through post method.
//i have used post method, you can use any method you want.

$recaptcha = $_POST['g-recaptcha-response'];

//After retrieving send a post curl request to verify it.
//you will have to send your secret key along with it.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=*******************************=".$recaptcha);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
$server_output = json_decode($server_output,true);
curl_close ($ch);

if($server_output['success']){
// captacha validated successfully.
}else{
// invalid captcha
}

Comments

Popular posts from this blog

How to do PHP unserialize jQuery serialized form data

How to do PHP unserialize jQuery serialized form data


  The Jquery serialize method just takes the form elements and puts them in string form. example - "firstvariable=value&secondvariable=othervalue";
if you are not using associative array then you can access this variable by $_GET and $_POST method.

Example = 
$variable1 = $_GET['firstvariable']; 

but if your data in an array then you need to unserialize jquery serialized data in php. you can unserialize jquery seralized data by usin this methods.

you can unserialize jquery serialize data into php by using parse_str.

//GET METHOD
$params = array(); parse_str($_GET, $params);//POST METHOD $params = array(); parse_str($_POST, $params);if your data serialize data in a variable then you need to use- $params = array(); parse_str($_POST['formdata'], $params);you can also use this function to unserialize your datafunction unserializeForm($str){ $returndata = array(); $strArray = explode("&",…

How to run query and num_rows in wordpress

How to run query and num_rows in wordpress

$query = $wpdb->get_results("SELECT wplc_postmeta.meta_id FROM wplc_postmeta WHERE 1 AND wplc_postmeta.meta_key = 'product_id' AND wplc_postmeta.meta_value = '$product_id'");
  $total_result_rows = $wpdb->num_rows;

  if($total_result_rows == 0)
       {
              //your code here
}


you can also use . $wpdb->prefix . if you dont know the table prefix
example-

function get_all_companies_selling( $product_id ){
    global $wpdb;
    $sql = "select post_id from " . $wpdb->prefix . "postmeta where
        meta_key = 'prefix_products' &&
        meta_value like '%%%s%%'";

    $product_id = 's:' . strlen( $product_id ) . ':"' . (int) $product_id . '";';
    $sql = $wpdb->prepare( $sql, $product_id );
    $res = $wpdb->get_results( $sql );

    return $res;
}

get_all_companies_selling( 331  );




 $args = array(
        'post_type'   =>…

Cakephp 3x Add TinyMCE text editor in 2 Steps

Cakephp 3.x Add text editor in 2 Steps
In this tutorial we will use TinyMCE because its easy to implement TinyMCE text editor

Step 1


So First Add this js to your header or default layout or your preferred location where you call other js files

<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>   <script> tinymce.init({ selector:'.edittextarea', height: 500,  plugins: [     'advlist autolink lists link print preview anchor',     'visualblocks code fullscreen',     'insertdatetime table contextmenu paste code'   ], }); </script>


Step 2
Now Add edittextarea class to textarea where you want to change textarea to text editor

Here is the example of how to create text editor textarea in Cakephp 3.x 

<?= $this->Form->input('description', array('type' => 'textarea','class' => 'edittextarea form-control','value'=>$description,'label' => …