โดยเข้าไปสมัครและรับ reCAPTCHA API keys ในลิ้งค์นี้ https://www.google.com/recaptcha
ตัวอย่างโค๊ดการใช้งาน
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php // reCAPTCHA secret key define('SecretKey', yourSecretKey); // allowed only POST method if ($_SERVER['REQUEST_METHOD'] == 'POST') { $query_params = [ 'secret' => SecretKey, 'response' => filter_input(INPUT_POST, 'g-recaptcha-response'), 'remoteip' => $_SERVER['REMOTE_ADDR'] ]; $url = 'https://www.google.com/recaptcha/api/siteverify?'.http_build_query($query_params); $result = json_decode(file_get_contents($url), true); if ($result['success']) { // TODO, when reCAPTCHA verify successfully } else { // TODO, when reCAPTCHA failed die("reCAPTCHA failed"); } } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title></title> <script src='https://www.google.com/recaptcha/api.js?hl=th'></script> </head> <body > <form method="post" action=""> <input type="text" name="description" /> <div class="g-recaptcha" data-callback="makeaction" data-sitekey="yourSiteKey"></div> <button id="btn_submit" type="submit" disabled="disabled">Submit</button> </form> <script> //reCAPTCHA function makeaction(){ document.getElementById('btn_submit').disabled = false; } // </script> </body> </html> |
อ้างอิง : https://www.nomkhonwaan.com/2015/08/08/how-to-use-new-recaptcha