google recaptcha using php validation-google reCAPTCHA v2

google recaptcha using php validation-google reCAPTCHA v2

In This post we will give you solution for google recaptcha using php. we will give you code for Google recaptcha client side javascript validation.

This way you’ll be able to handle the validation of the response in your kind submit handler mistreatment javascript. This appears a lot of easy resolution than attempting to handle it on the captcha asking.

How to register for Google No CAPTCHA
Step 1 :-

First, we have to create API key, so we have to go https://www.google.com/recaptcha/admin create. Add label and domain register site in google recaptcha admin.

 

add label and domain register site in google recaptcha admin.
dd label and domain register site in google recaptcha admin.

Step 2 :-

We get key and its partner secret key ::

google Invisible reCAPTCHA using php key
google Invisible reCAPTCHA using php key

Step 3 :-

Add your recaptcha key of google api.js ::

<!-- recaptcha js script -->
<script src='https://www.google.com/recaptcha/api.js'></script>

Step 4 :-

Add this site key in div data-sitekey="add-hear"


<button class="g-recaptcha"
data-sitekey="yoursitekey"
data-callback="onSubmit">Submit
</button>

google recaptcha using php validation

this is code for google recaptcha using php validation for for Google recaptcha client side javascript validation to check are you a robot?

create configs.php file

 'Add your client key',
	'secret_key' => 'Add your secret key'
];

create login.php file (View Page)



  
    google recaptcha using php validation - onlinecode
    
	 
  
  
    

Codes for admin_submit.php file (form action)

verifyResponse($recaptcha_val);

if(isset($response_val['success']) and $response_val['success'] != true) 
{
	echo "An Error Occured and Error code is :".$response_val['error-codes']." Try again";
}else 
{
	echo "Correct Recaptcha you can do your process";
	// add you code hear
}

Now the main PHP library reCAPTCHA created by me. create Recaptchas.php file

configs = require('configs.php');
    }

	public function verifyResponse($recaptcha_val){
		
		$getremoteIp = $this->getIPAddress();

		// Discard empty solution submissions
		if (empty($recaptcha_val)) {
			return array(
				'success' => false,
				'error-codes' => 'missing-input',
			);
		}

		$getResponse = $this->getHTTP(
			array(
				'secret' => $this->configs['secret_key'],
				'remoteip' => $getremoteIp,
				'response' => $recaptcha_val,
			)
		);

		// get reCAPTCHA server response
		$responses = json_decode($getResponse, true);

		if (isset($responses['success']) and $responses['success'] == true) {
			// status true
			$status = true;
		} else {
			// status false
			$status = false;
			// status error
			$error = (isset($responses['error-codes'])) ? $responses['error-codes']
				: 'invalid-input-response';
		}
		// return array of ip
		return array(
			// status return in array 
			'success' => $status,
			'error-codes' => (isset($error)) ? $error : null,
		);
	}

	// get IP Address
	private function getIPAddress(){
		// get ip
		if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
		{
			$ip_add = $_SERVER['HTTP_CLIENT_IP'];
		} 
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
		{
			// get ip
			$ip_add = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} 
		else 
		{
			// get ip
			$ip_add = $_SERVER['REMOTE_ADDR'];
		}
		// return ip
		return $ip_add;
	}

	// get HTTP	data
	private function getHTTP($datas){
		// get url
		$get_url = 'https://www.google.com/recaptcha/api/siteverify?'.http_build_query($datas);
		$response_data = file_get_contents($get_url);
		// return HTTP data
		return $response_data;
	}
}
?>

You also like google recaptcha using javascript and google recaptcha using php

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  60  =  65

We accept paid advance and paid guest Posting on our Site : Contact US