Setelah sebelumnya kita telah membahas Cara Membuat Captcha Google Di PHP Bagian 1 yang mana membahas tentang bagaimana cara registraso API ke google agar dapat menggunakan captcha google di website kita.
Di artikel ini saya akan memberikan contoh penggunaanya pada real aplikasi nya :
Pertama-tama sisipkan google api nya dulu yang ini :
|
1 |
<script src='https://www.google.com/recaptcha/api.js'></script> |
Lalu buat form nya seperti ini :
|
1 2 3 |
<div class="form-group"> <div class="g-recaptcha" data-sitekey="<?php echo $site_key; ?>"></div> </div> |
Setelah itu berikut adalah contoh untuk proses di bagian PHP nya :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php $site_key = 'xxxxxxxxxxx'; // Diisi dengan site_key API Google reCapthca yang sobat miliki $secret_key = 'xxxxxxxxxxx'; // Diisi dengan secret_key API Google reCapthca yang sobat miliki if (isset($_POST['submit'])) { if(isset($_POST['g-recaptcha-response'])) { $api_url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response='.$_POST['g-recaptcha-response']; $response = @file_get_contents($api_url); $data = json_decode($response, true); if($data['success']) { $comment = $_POST['komentar']; // proses penyimpananan dan lain-lain disini $success = true; } else { $success = false; } } else { $success = false; } } ?> |
Baiklah itu saja untuk artikel tentang Cara Membuat Captcha Google Di PHP Bagian 2, sampau bertemu pada artikel selanjutnya.