Membuat Captcha dengan Google reCaptcha v2

Membuat Captcha dengan Google reCaptcha v2

Diartikel kali ini kita akan mencoba membuat sebuah fitur pada form dengan menambahkan captcha dengan Google reCaptcha. Untuk artikel sebelumnya pernah juga dibahas fitur serupa, namun dengan teknik yang sederhana, dapat dilihat pada artikel dengan judul membuat-simple-captcha-dengan-php.

Untuk demonya kalian dapat lihat pada link berikut:

https://www.yellowweb.id/demo/google-recaptchav2/index.php

	<?php

	/* Author: Joshua Herbison of Idea Pro LLC, IdeaPro.com */
	/* The code below is only for an example and testing purposes.*/
	/* With API keys, this is a fully functional and working form, but it does not send the information
		anywhere. This code is "Use at your own risk". Idea Pro LLC and/or Joshua Herbison is not responsible
		for any developer's misuse of any code we provide in Tutorials. By using this code you agree to those terms.*/ 

	$public_key = "6Lec2vAUAAAAANALTewxuPInF1Ig3zkuyQwCWbls"; /* Your reCaptcha public key */
	$private_key = "6Lec2vAUAAAAAI6SipAebT8xjAq6x7c0Qj75E9aF"; /* Enter your reCaptcha private key */
	$url = "https://www.google.com/recaptcha/api/siteverify"; /* Default end-point, please verify this before using it */

	/* Check if the form has been submitted */
	if(array_key_exists('submit_form',$_POST))
	{
		/* The response given by the form being submitted */
		$response_key = $_POST['g-recaptcha-response'];
		/* Send the data to the API for a response */
		$response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']);
		/* json decode the response to an object */
		$response = json_decode($response);

		/* if success */
		if($response->success == 1)
		{
			echo "You passed validation!";
		}
		else
		{
			echo "You are a robot and we don't like robots.";
		}
	}
	
?>
<!DOCTYPE html>
<html>
<head>
	<title>Google reCaptchaV2 | Kursus Web YELLOWWEB</title>
</head>
<body>


<form method="post" action="">
<input type="text" name="your_name" placeholder="Your Name" /><br /><br />
<input type="text" name="email" placeholder="Your Email Address" /><br /><br />
<div class="g-recaptcha" data-sitekey="<?php print $public_key; ?>"></div>
<input type="submit" name="submit_form" value="Submit Your Information" />
</form>	
<script src='https://www.google.com/recaptcha/api.js'></script>

</body>
</html>

disini mengambil sumber dari vlog Joshua, yang pertama teman-teman lakukan adalah login dahulu ke google recaptcha dan masuk ke admin console untuk menggunakan api google.

pada saat pembuatan tutorial captcha ini, posisi web sudah dalam keadaan dihosting (live).