2.表示用部分に記載する
3.プログラムに記載する
以上簡単3ステップ!
1.Google RecaptchaサイトにてGoogle reCAPTCHAのkeyを取得する
- Google Recaptchaサイトを開き 【リンク】 コンソールへ入る
 - 画面内の+より新規サイト追加

 - 設置するサイトのドメインやreCAPTCHAタイプ等を入力して送信ボタンを押す

 - サイトキーとシークレットキーが発行されるのでそれを控える

 
2.表示用部分に記載する
- reCAPTCA ドキュメントに記載されているコードを参考に自分のコンタクトフォームのhead内に記載する 【https://developers.google.com/recaptcha/docs/v3】
記載例
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>お問い合わせフォーム</title>
<script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('reCAPTCHA_site_key', {action: 'homepage'}).then(function(token) {
var recaptchacode= document.getElementById('recaptchacode');
recaptchacode.value = token;
});
});
</script>
</head>
<body>
<form method="post" action="POST" target="_top" class="contactForm">
<input type="hidden" name="recaptchacode" id="recaptchacode">
コンタクトフォーム
</form>
</body>
</html> 
3.プログラムに記載する
 
- 処理するPHPプログラムに記載する (下記はサンプルです)
if (isset($_POST['recaptchaResponse']) && !empty($_POST['recaptchaResponse']))
{
$secret = 'reCAPTCHA_secret_key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&recaptchacode='.$_POST['recaptchacode']);
$reCAPTCHA = json_decode($verifyResponse);
if ($reCAPTCHA->success)
{
// OK の場合の処理
if(($_POST['Name']) == TRUE){
$data['Name'] = htmlentities($_POST['Name'] );
}
if(($_POST['yourName']) == TRUE){
$data['yourName'] = htmlentities($_POST['yourName'] );
}
if(($_POST['yourEmail']) == TRUE){
$data['yourEmail'] = htmlentities($_POST['yourEmail'] );
}
$data['phoneNumber'] = htmlentities($_POST['phoneNumber'] );
$data['comment'] = htmlentities($_POST['comment'] );
//メール送信処理
$mailing->mailsend($data);
exit;
}
else
{
// NG の場合の処理
redirect(BaseUrl);
}
}