【问题标题】:Execute phpmailer script only if google captcha is successfully executed仅在成功执行 google captcha 时执行 phpmailer 脚本
【发布时间】:2018-07-06 15:06:51
【问题描述】:

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

有没有办法只有在用户正确执行recaptcha的情况下才能执行sendmail.php?

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

这是google验证码的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 )

}
?>

谢谢你帮助我:)

【问题讨论】:

  • 您的方向完全正确。不要害怕尝试!

标签: php phpmailer recaptcha


【解决方案1】:

您可以在验证码成功时包含像require('./sendmail.php'); 这样的sendmail.php 文件,或者只需复制代码并将其粘贴到验证码成功区域。;

【讨论】:

  • 谢谢你的建议,我试试这个方法
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 2021-10-09
  • 2015-08-27
  • 2021-12-02
  • 2018-12-03
  • 1970-01-01
  • 2021-04-10
  • 2020-01-12
相关资源
最近更新 更多