【问题标题】:Cant get google recaptcha v2 to prevent form submission无法获取 google recaptcha v2 以阻止表单提交
【发布时间】:2015-05-13 17:23:31
【问题描述】:

所以我可以使用以下代码成功获取验证码进行验证。

  </p>
<?php
if(isset($_POST['g-recaptcha-response'])){
echo verify($_POST['g-recaptcha-response']);

}

function verify($response) {
$ip = $_SERVER['blank']; //server Ip
$key="secretkey"; // Secret key

//Build up the url
$url = 'https://www.google.com/recaptcha/api/siteverify';
$full_url = $url.'?secret='.$key.'&response='.$response.'&remoteip='.$ip;

//Get the response back decode the json
$data = json_decode(file_get_contents($full_url));
//Return true or false, based on users input
if(isset($data->success) && $data->success == true) {
return True;
}
return False;
}
?>
<p style="text-align: justify;">
<script type="text/javascript">
 function verify(){
 var serializedValues = jQuery("#infoForm").serialize();
 jQuery.ajax({ type: 'POST',url:"verify.php",data: serializedValues,success:function(result){
 if(result){
 $('#show').html('Your Form Successfully Submitted');
 $('.formwrap').hide(result);
 return true;
 }
 }});
 $('#show').html('Please Enter Valid Captcha');
 return false;
}
 var onloadCallback = function() {
 grecaptcha.render('captcha_ele', {
 'sitekey' : 'Enter Your Site Key Here', // Site key
 });
 };
 </script>

但是,当我点击提交时,无论验证码说什么,表单仍然会提交。我的邮件表单流程如下...

<!-- language: lang-css -->

$("#blank").submit(function() {
    $.post('assets/php/email-process.php', {name: $('#name').val(), email: $('#email').val(), message: $('#message').val(), myFormSubmitted: 'yes'}, 
    function(data) {
        $("#formResponse").html(data).fadeIn('100');
        $('#name, #email, #message').val(''); /* Clear the inputs */
    }, 'text');
    return false;
}); 
<?php
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
   // display message that the form submission was rejected
}
else {
   // accept form submission
$to = 'info@blank'; // Replace with your email  
    $subject = 'Message from website visitor'; // Replace with your $subject
    $headers = 'From: ' . $_POST['email'] . "\r\n" . 'Reply-To: ' . $_POST['email'];    

    $message = 'Name: ' . $_POST['name'] . "\n" .
               'E-mail: ' . $_POST['email'] . "\n" .
               'Subject: ' . $_POST['subject'] . "\n" .
               'Message: ' . $_POST['message'];

    mail($to, $subject, $message, $headers);    
    if( $_POST['copy'] == 'on' )
    {
        mail($_POST['email'], $subject, $message, $headers);
    }
    echo 'Thank you for your Email. We will get in touch with you very soon.';

}


?>

【问题讨论】:

    标签: php jquery html captcha


    【解决方案1】:

    我用这个对我有用。在您的表单提交中放置一个 js 函数以验证重新验证码:

    <form action="/sign-visitors-log/" method="post" id="VisitorsLogForm" onsubmit="return validateRecaptcha();">
    

    如果用户没有勾选复选框,那么一些js会停止表单提交:

    function validateRecaptcha() {
        var response = grecaptcha.getResponse();
        if (response.length === 0) {
            alert("not validated");
            return false;
        } else {
            alert("validated");
            return true;
        }
    }
    

    您可以将警报换成 toast 或当您在页面上执行某些元素时。

    HTH

    【讨论】:

    • 太棒了!谢谢
    • 很高兴能帮上忙。也许你可以投票给我的答案?
    • 为我工作!
    • 不错,快速简单,需要。提交后不断寻找更多示例。
    • 这就是为什么您还需要防伪令牌。但这是对另一个问题的回答。
    【解决方案2】:

    在单独的 js 文件中(至少:没有对函数的内联调用),您必须检查验证码是否可以验证。像这样:

      jquery('form').on('submit',function(e){
    
          if(grecaptcha.getResponse() === "") {
            e.preventDefault();
            alert('Error: \n please validate the Captcha test');
         }
    
      });
    

    您不必检查测试被动是否为真,您已经阻止使用此方法发送表单。

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多