<?php
/**
* Botチェック
* @return bool
*/
function is_Bot() {
if (isset($_POST["g-recaptcha-response"])) {
$recaptcha = htmlspecialchars($_POST["g-recaptcha-response"],ENT_QUOTES,'UTF-8');
$captcha = $recaptcha;
$secretKey = "{シークレットキー}";
$resp = @file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$captcha}");
$resp_result = json_decode($resp,true);
if ($resp_result['success'] && $resp_result['score'] > 0.3) return false;
}
if (isset($_POST['captcha_code'])) {
require_once('/securimage/securimage.php');
$securimage = new Securimage();
if ($securimage->check(filter_input(INPUT_POST, 'captcha_code'))) {
return false;
}
#var_dump($securimage->check(filter_input(INPUT_POST, 'captcha_code')));
#var_dump($_POST['captcha_code']);
}
return true;
}
?>
<?php
# recaptcha v3
$captchaErrorFlag = false;
# 入力値のバリデーション
if($formBuilder->inputValidater()) {
$captchaErrorFlag = is_Bot();
if($action === 'submit' && !$captchaErrorFlag) {
/* 送信OK時の処理を書く */
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<?php // reCAPTCHA3 ?>
<script src='https://www.google.com/recaptcha/api.js?render={サイトキー}'></script>
</head>
<body>
<?php if ($captchaErrorFlag) : #reCAPTCHA認証エラー時の画像認証処理?>
<?php #print_r($resp_result); #var_dump($_SESSION);?>
<?php #画像認証 ?>
<div class="row gutters cf mt30" style="max-width: 600px; margin-left: auto; margin-right: auto; border: 1px solid #ccc; padding: 1em; background-color: #ccc;">
<div class="col_4">
<img id="captcha" src="/securimage/securimage_show.php">
</div>
<div class="col_8 sml">
表示されているテキストを入力してください。
<?php
if (isset($_POST['captcha_code'])) {
require_once('/securimage/securimage.php');
$securimage = new Securimage();
if (!$securimage->check(filter_input(INPUT_POST, 'captcha_code'))) {
echo '<div style="color: #f00;">もう一度入力してください。</div>';
}
}
?>
<input type="text" name="captcha_code">
<button type="button" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;" style="width: auto; margin: 0; padding: 0; text-decoration: underline;" class="sml">画像を再生成する</button>
</div>
</div>
<?php endif; ?>
<?php // reCAPTCHA3 ?>
<script>
function onSubmit(element, event){
var formElement = document.form1;
formElement.insertAdjacentHTML('beforeend', '<input type="hidden" name="actiontype" value="' + element.value + '">');
grecaptcha.ready(function() {
grecaptcha.execute('{サイトキー}', {action: 'mail_form'})
.then(function(token) {
//トークン取得が成功した場合
formElement.insertAdjacentHTML('beforeend', '<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
formElement.submit(); //実際にサーバーに送信
});
});
}
</script><?php // reCAPTCHA3ここまで ?>
</body>
</body>
</html>