Recaptcha invalid-request-cookie

时间:2013-12-06 06:26:02

标签: javascript html google-api captcha recaptcha

我正在尝试在我的页面中执行Recaptcha。我正在查看localhost的演示。但是,我总是在检查时始终收到错误invalid-request-cookie。我正在关注Displaying recaptcha without pluginVerifying recaptcha without plugin

这是我的代码

<html>
<body>
<form method="post" action="http://www.google.com/recaptcha/api/verify">
<script type="text/javascript"
 src="http://www.google.com/recaptcha/api/challenge?k=my_public_key">
 <!-- I used my public key -->
</script>
<noscript>
 <iframe src="http://www.google.com/recaptcha/api/noscript?k=my_public_key"
     height="300" width="500" frameborder="0"></iframe><br>
 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
 </textarea>
 <input type="hidden" name="recaptcha_response_field"
     value="manual_challenge">
</noscript>
<input type="hidden" name="privatekey" value="my_private_key">
<!-- I used my private key -->
<input type="submit" value="Ok"/>
</form>
</body>
</html>

在Google中,我看到了,invalid-request-cookie表示The challenge parameter of the verify script was incorrect。但这似乎是正确的。是对还是还有其他错误?有人请帮忙

2 个答案:

答案 0 :(得分:0)

在阅读this之后,我意识到我们其中一个表单的作者使用了一个我们也拥有的不同域的公钥。因此,请确保您使用正确的公钥。

答案 1 :(得分:0)

我在ASP.Net环境中使用Google recaptcha。这是我的代码片段:

头标记:

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

HTML:

        <div class="g-recaptcha" data-sitekey="My***PUBLIC***SiteKeyBlahBlah"></div>

就是这样!谷歌处理其余的魔术。您可以检查grecaptcha.getResponse()函数的返回变量的长度,以查看用户是否单击了它。例如:

if (grecaptcha.getResponse().length == 0)
     //They didn't do it
else
     //They either did it or spoofed your page with some bogus HTML to make it look like they did - they can do this by editing the source of the page and inserting text in a certain spot.  View your page source after loading in a browser to see what I mean.

要验证他们不只是输入随机文本 - 并且grecaptcha.getResponse()的值是来自Google的有效回复,只需使用您的网站密钥调用他们的网络服务 - 以及响应本身。我正在使用C#代码隐藏这样做:

WebRequest CheckCaptcha;
            CheckCaptcha = WebRequest.Create(String.Format([Google's Web Service URL],
                                            [Your ***Private*** Site Key],
                                            [The value of grecaptcha.getResponse()],
                                            [IP address]));
            Stream strm = CheckCaptcha.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            string everything = sr.ReadToEnd();

            JavaScriptSerializer JS = new JavaScriptSerializer();
            CaptchaResponse GoogleResponse = JS.Deserialize<CaptchaResponse>(everything);

接下来,评估Google的回复:

if (GoogleResponse.success.ToUpper() != "TRUE")
    //Invalid - they are up to no good! 
else
    //Valid - you're good to go!

如果您是从客户端进行的,那么调用他们的Web服务可能会略有不同,但这是相同的原则。我希望这会有所帮助。

相关问题