Google ReCaptcha not posting 'g-recaptcha-resp

2019-04-03 21:32发布

This question has been asked before: New Google ReCaptcha not posting/receiving 'g-recaptcha-response' - but there was no proper answer.

I have the exact same set up as him, but the code fails here:

if(!$captcha){
   exit;
}

so $captcha=$_POST['g-recaptcha-response'] seems to be empty.

new google recaptcha with checkbox server side php = The 2nd answer here also doesn't seem to work.

Does anyone know why this could happen?

标签: php recaptcha
10条回答
你好瞎i
2楼-- · 2019-04-03 22:12

Check if you have the following present in the part where you show the form to the user:

  1. Between <form> and </form>:

    <div class="g-recaptcha" data-sitekey="your_public_key"></div>

  2. Before the closing </head> tag:

    <script src='https://www.google.com/recaptcha/api.js'></script>

  3. Check that your form uses post as method, ...

    <form method="post" ...>

If these are correct, at least some $_POST['grecaptcha-response'] should be coming your way. Check those first in the resulting client side html code (in many browsers by pressingStrg+U while looking at the user-form) - rather than your server side code - it's easier to work with that knowledge. If all of those are in place even at the client, this will however be a tough one ^^

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-03 22:13

I encountered this issue and found that my form was closing prematurely in the DOM because it was inside a table. ReCaptcha sets up a display:none g-recaptcha-response textarea and later fills in the data when you complete the captcha. It seems to look for children of the form that the div is in and thus couldn't find the g-recaptcha-response it had initially created. I put the form around the table and it worked fine after that.

查看更多
混吃等死
4楼-- · 2019-04-03 22:19

If you are using reCaptcha v3, you have to explicitly define g-recaptcha-response in your form by attaching an ID to it.

If you are using reCaptcha v2, just make sure to put recaptchacontainer with in your form

查看更多
Juvenile、少年°
5楼-- · 2019-04-03 22:19

None of the above worked for me. I'm using react-google-recaptcha. And it seems like you have to await/resolve the recaptchaRef.current.execute() Promise beforehand.

Without resolving the promise, it worked half the times, and that is when I call recaptchaRef.current.getValue(). Otherwise it would return an empty value.

查看更多
The star\"
6楼-- · 2019-04-03 22:24

Just had the same problem. It was not a <table> tag causing the problem but it was a <div> tag causing the problem.

My form was within a main <div> used to format the general layout of the form. The <form> tag doesn't HAVE to be within the main <div> I was using for the form layout. I moved the <form> tag just before my form layout <div> tag and it started working perfectly.

查看更多
闹够了就滚
7楼-- · 2019-04-03 22:26

First check if recaptcha is set

if(!isset($_POST['g-recaptcha-response']) ){
    die ("Error: Not valid recaptcha on form");
}

Also have a look at this simple PHP tutorial for simple debugging.

查看更多
登录 后发表回答