仅在成功执行google验证码时才执行phpmailer脚本

时间:2018-01-27 11:55:15

标签: php phpmailer recaptcha

我需要将google的隐形recaptcha php脚本集成到一个网站中,该网站的表单与phpmailer脚本一起使用,我称之为sendmail.php(form action =“sendmail.php”)。

只有在用户正确执行recaptcha时才能执行sendmail.php吗?

我有两个php脚本,但我是php的新手,我不知道如何将它们结合起来:(。

以下是谷歌验证码的php脚本

<?php
function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => '________________SECRET_KEY_______________',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

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

    return json_decode($result, true);
}

// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
    // What happens when the reCAPTCHA is not properly set up
    echo 'reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.';
} else {
    // If CAPTCHA is successful...

    // Paste mail function or whatever else you want to happen here!
    echo '<br><p>CAPTCHA was completed successfully!</p><br>';

    // Here, I suppose, I need to execute sendmail.php ( phpmailer script )

}
?>

感谢您帮助我:)

1 个答案:

答案 0 :(得分:0)

当验证码成功时,您可以包含require('./sendmail.php');之类的sendmail.php文件,或者只是复制代码并将其通过验证码成功区域。