Google reCAPTCHA (กูเกิล รีแคปช่า) คืออะไร
Google reCAPTCHA (กูเกิล รีแคปช่า) คือ ระบบป้องกันโปรแกรมอัตโนมัติ (Bot) หรือสแปมเพื่อไม่ให้เข้ามาใช้งานเว็บไซต์หรือระบบของเรา นอกจากนี้กูเกิลยังพัฒนา Android API ใหม่ของ reCAPTCHA
วิธีการใส่ Google reCAPTCHA ในไฟล์ php
สามารถเรียนรู้ วิธีการเข้าใช้งาน Google reCAPTCHA ก่อนการเขียน php
ตัวอย่าง index.php หน้า รับค่าข้อมูล
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Google reCAPTCHA (mindPHP)</title>
<script src='https://www.google.com/recaptcha/api.js?hl=th'></script> // นำมาจาก Google reCAPTCHA
</head>
<body>
$keyrecaptcha = ใส่ sitekey ตรงนี้;
<form name="form" method="post" action="form.php">
<div>Input: </div>
<div>
<textarea name="m" cols="50" rows="3" id="m"></textarea>
</div>
<script> // กำหนดปุ่มเป็น disable ไว้ ต้องทำ reCHAPTCHA ก่อนจึงกดได้
function makeaction(){
document.getElementById('submit').disabled = false;
}
</script>
<div class="g-recaptcha" data-callback="makeaction" data-sitekey="<?php echo $keyrecaptcha; ?>""></div>
<div>
<br>
<button type="submit" id="submit" name="btn_submit" disabled>Submit</button>
</div>
</form>
</body>
</html>
ตัวอย่าง form.php หน้า ตรวจสอบรับค่าข้อมูลที่ส่งเข้ามา
<?php
session_start();
$secretkeyrecaptcha = ใส่ secretkey ตรงนี้;
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=.$secretkeyrecaptcha."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if(!$captcha){
$missinginputsecret = ["The response parameter is missing."];
print_r($missinginputsecret[0]);}
}
if(isset($_POST['submit']) && $response['success'] == true){
// เมื่อคลิกที่กที่ปุ่ม
/// ทำคำสั่งเพิ่มข้อมูลหรืออื่นๆ
}
?>
ผลลัพธ์
เมื่อทำ reCAPTCHA ผ่านจะสามารถกดปุ่ม Submit ได้และจะส่งค่าไปที่หน้า form.php
ปัจจุบันมีเวอร์ชั่นใหม่ดูได้จากบทความนี้ ป้องกันสแปมโพส สแปมสมัครสมาชิก บน php ด้วย Google reCAPTCHA เวอร์ชั่น 3
ข่าวสารเพิ่มเติมเกี่ยวกับ Google