Google reCAPTCHA php sample code

Google 推出了驗證是否為機器人的服務
以下這段是跟 Google 驗證資料是否正確的程式


$captcha = $_POST['g-recaptcha-response'];

$secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$response = validate_google_recaptcha($secret_key, $captcha);

if ($response->success == false) {
 echo 'You are spammer ! Get the @$%K out';
}
else {
 echo 'Thanks for posting comment.';
}

function validate_google_recaptcha($secret_key, $captcha) {
 $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
 curl_setopt($ch, CURLOPT_HEADER, false);

 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

 $param = "secret=".$secret_key."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR'];

 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

 $temp = curl_exec($ch);
 curl_close($ch);

 return json_decode($temp);
}

Comments

Popular posts from this blog

CentOS7 如何新增/移除 Rich Rule for firewalld

mac 安裝 mtr

如何設定 SSL VPN 分割通道 on Fortigate 80C