php Google ReCaptcha 未发布“g-recaptcha-response”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30685443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google ReCaptcha not posting 'g-recaptcha-response'
提问by frosty
This question has been asked before: New Google ReCaptcha not posting/receiving 'g-recaptcha-response'- but there was no proper answer.
以前曾问过这个问题:新的 Google ReCaptcha 未发布/接收“g-recaptcha-response”- 但没有正确的答案。
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.
所以$captcha=$_POST['g-recaptcha-response']
似乎是空的。
new google recaptcha with checkbox server side php= The 2nd answer here also doesn't seem to work.
带有复选框服务器端 php 的新 google recaptcha= 这里的第二个答案似乎也不起作用。
Does anyone know why this could happen?
有谁知道为什么会发生这种情况?
回答by Kiwimancy
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.
我遇到了这个问题,发现我的表单在 DOM 中过早关闭,因为它在一个表中。ReCaptcha 设置一个 display:none g-recaptcha-response textarea,然后在您完成验证码时填写数据。它似乎在寻找 div 所在形式的子级,因此找不到它最初创建的 g-recaptcha-response。我把表格放在桌子周围,之后它工作得很好。
回答by zed
Today I had this same issue (g-recaptcha-response
had no value upon submit) on a website of a colleague. Turns out that the tag <form
was mistakenly nested right after the opening tag <table
(not inside a td, but directly after <table
).
This was causing the issue.
After moving the tag form
in way it wrapped the table
, the value of g-recaptcha-response
was correctly posted to server side after submit.
今天我在g-recaptcha-response
同事的网站上遇到了同样的问题(提交时没有价值)。结果是该标签<form
错误地嵌套在开始标签之后<table
(不在 td 内,而是直接在 之后<table
)。
这导致了这个问题。以包装 的方式
移动标签后,提交后的值被正确发布到服务器端。form
table
g-recaptcha-response
回答by Levite
Check if you have the following present in the part where you show the form to the user:
检查在向用户显示表单的部分中是否存在以下内容:
Between
<form>
and</form>
:<div class="g-recaptcha" data-sitekey="your_public_key"></div>
Before the closing
</head>
tag:<script src='https://www.google.com/recaptcha/api.js'></script>
Check that your form uses
post
as method, ...<form method="post" ...>
间
<form>
和</form>
:<div class="g-recaptcha" data-sitekey="your_public_key"></div>
在结束
</head>
标记之前:<script src='https://www.google.com/recaptcha/api.js'></script>
检查您的表单是否
post
用作方法,...<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 ^^
如果这些是正确的,那么至少有一些$_POST['grecaptcha-response']
应该是你的方式。首先检查生成的客户端 html 代码中的那些(在许多浏览器中,通过在查看用户表单时按Strg
+U
键) - 而不是您的服务器端代码 - 使用这些知识更容易。如果所有这些甚至在客户端都到位,那么这将是一个艰难的过程^^
回答by integriz
Just had the same problem. It was not a <table>
tag causing the problem but it was a <div>
tag causing the problem.
刚刚有同样的问题。它不是<table>
导致问题的标签,而是导致问题的<div>
标签。
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.
我的表单位于一个 main 中,<div>
用于格式化表单的总体布局。该<form>
标记不必须是内主要的<div>
,我使用的形式布局。我<form>
在表单布局<div>
标签之前移动了标签,它开始完美地工作。
回答by Wittner
It seems that Google wants your opening and closing tags to be outside of other DOM elements such as <table>
or <div>
. I had the exact same issue which is now resolved. Move your...
Google 似乎希望您的开始和结束标记位于其他 DOM 元素之外,例如<table>
或<div>
。我遇到了完全相同的问题,现在已解决。移动你的...
<div class="g-recaptcha" data-sitekey="abcd1234etc."></div>
...code to outside of any or tag and it will work. Seems as if Google cannot find the form and inject its form value otherwise.
...代码到任何或标签之外,它会起作用。似乎 Google 找不到该表单并以其他方式注入其表单值。
回答by Mr.Nestorjava
The perfect solution is do not create your own g-recaptcha-responseinput because google will fill it with the response and still yet go ahead creating another g-recaptcha-responsetextarea but then will not fill it with the response value
完美的解决方案是不要创建自己的g-recaptcha-response输入,因为谷歌会用响应填充它,但仍然继续创建另一个g-recaptcha-response文本区域,但不会用响应值填充它
回答by Ravi
If you are using reCaptcha v3, you have to explicitly define g-recaptcha-responsein your form by attaching an ID to it.
如果您使用的是 reCaptcha v3,则必须通过在表单中附加 ID 来明确定义g-recaptcha-response。
If you are using reCaptcha v2, just make sure to put recaptchacontainer with in your form
如果您使用的是 reCaptcha v2,请确保将 recaptchacontainer with 放入您的表单中
回答by Charming Robot
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.
以上都不适合我。我正在使用react-google-recaptcha
. 似乎您必须recaptchaRef.current.execute()
事先等待/解决Promise。
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.
没有解决承诺,它工作了一半,那是我打电话的时候recaptchaRef.current.getValue()
。否则它将返回一个空值。
回答by Héctor Ocampo
Yes, the error is the DOM
是的,错误是 DOM
Code error
代码错误
table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"
form id="contacto" name="contacto" method="post" action="xxx"
Code OK
代码正常
form id="contacto" name="contacto" method="post" action="xxx"
table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"
回答by XIMRX
First check if recaptcha is set
首先检查是否设置了recaptcha
if(!isset($_POST['g-recaptcha-response']) ){
die ("Error: Not valid recaptcha on form");
}
Also have a look at this simple PHP tutorialfor simple debugging.
还可以查看这个简单的 PHP 教程以进行简单的调试。