在本地主机上使用 reCAPTCHA
我正在使用 PHP 开发一个网站,我想在其中一个会话中进行人工验证。对于开发,我最初在本地运行系统,当它准备就绪时,我将把它放在某个域上。
在 reCAPTCHA 网站 中,据说该插件只能在给定的域中运行(并且子域)。
有没有办法在本地主机上使用 reCAPTCHA 插件?
I'm developing a website using PHP and I want to make a human verification in one of the sessions. For the development, I'm initially running the system locally and when it is ready, I'm going to put it on some domain.
In the reCAPTCHA website it is said that the plugin will only work at the given domain (and subdomains).
Is there a way to use the reCAPTCHA plugin on a localhost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
更新
原来的答案不再正确。开发者指南现在指出:
仅当您使用
127.0.0.1/...
而不是localhost/...
访问 localhost 时,此功能才有效。原答案保留如下。
根据reCAPTCHA 开发者指南:
换句话说,只需使用相同的密钥即可。
Update
The original answer is no longer correct. The developer's guide now states:
This will only work if you access localhost using
127.0.0.1/...
rather thanlocalhost/...
.The original answer is preserved below.
According to the reCAPTCHA Developer's Guide:
In other words, simply use the same key.
这对我有用:
摘自 reCAPTCHA 文档:
This worked for me:
Extracted from the reCAPTCHA documentation:
请注意,自 2016 年起,reCAPTCHA 不再原生支持 localhost。来自常见问题解答:
只需将
localhost
添加到您网站的域列表中即可。Please note that as of 2016, reCAPTCHA doesn't natively support localhost anymore. From the FAQ:
Just add
localhost
to your list of domains for your site and you'll be good.非常简单:
localhost
& ;127.0.0.1
添加到新站点的域,如下图所示。更新:
如果您的问题是如何在 Google 网站上设置 reCAPTCHA 以在本地主机中使用它,那么它就像我上面写的那样,但如果您好奇如何您可以通过控制器中的最少代码在两者
localhost
和网站主机
上使用reCAPTCHA,并防止某些代码,例如其中的ConfigurationManager.AppSettings["ReCaptcha:SiteKey"]
,然后我在答案中帮助您提供额外的描述和代码。您喜欢以下 GET 和 POST 操作吗?
下面的代码支持 reCAPTCHA,并且不需要任何其他代码来处理 reCAPTCHA。
在查看:(参考)
至使用它
A) 将以下
ActionFilter
添加到您的 Web 项目:B) 为两个
添加 reCAPTCHA 设置密钥localhost
和website
在您的webconfig
文件中如下所示:注意: 通过这种方式,您不需要设置
reCaptcha_SecretKey post 操作 中的
参数或在操作和视图中手动输入 reCaptcha 的任何ViewBag
参数。所有这些都将在运行时自动填充适当的值,具体取决于您是否在本地主机或网站上运行项目。It's so easy:
localhost
&127.0.0.1
to the domains of a new site, like the following image.Update:
If your question is how to set reCAPTCHA on the Google site for using it in localhost, then it has be as I wrote it above, but if you are curious how you can use reCAPTCHA on both
localhost
and awebsite host
by minimal code in your controller and prevent some codes likeConfigurationManager.AppSettings["ReCaptcha:SiteKey"]
in it, then I help you with this extra description and code in my answer.Do you like the following GET and POST actions?
The code below supports reCAPTCHA and doesn't need any other code for handling reCAPTCHA.
In View: (reference)
To use it
A) Add the following
ActionFilter
to your Web project:B) Add the reCAPTCHA settings keys for both
localhost
andwebsite
like this in yourwebconfig
file:Note: By this way you did not need set the
reCaptcha_SecretKey
parameter in the post action or anyViewBag
for reCaptcha manually in your Actions and Views. All of them will be filled in automatically at runtime with appropriate values depending on if you have run the project on the localhost or website.当您在 Google reCAPTCHA 中添加域时:
添加新域:
Localhost,而不是localhost
“L”是大写字母。
When you add domains in Google reCAPTCHA:
Add new domain:
Localhost, not localhost
"L" is a capital letter.
自 2021 年 1 月 2 日起,Google 在本文中发布了这两个密钥以进行测试。
我第一次使用密钥时,生成的电子邮件进入了垃圾邮件。
As of January 2nd, 2021, Google posted these two keys for testing in this article.
The generated emails went into spam the first time I used the keys.
删除当前的 reCAPTCHA 密钥,注册新密钥,并使用以下域设置您的密钥设置:
Remove the current reCAPTCHA key, register the new key, and set your key settings with these domains:
2022 年最新更新
默认情况下不支持本地主机域。如果您希望继续支持它们的开发,您可以将它们添加到您的站点密钥的支持域列表中。
https://developers.google.com/recaptcha/docs/faq#im-getting-an-error-localhost-is-not-in-the-list-of- support-domains.-what-should-i-do
我添加了“localhost”,它工作得很好。
或
对于 reCAPTCHA v2,请使用以下测试密钥。您将始终获得“无验证码”,并且所有验证请求都会通过。
参考
Latest update 2022
Localhost domains are not supported by default. If you wish to continue supporting them for development, you can add them to the list of supported domains for your site key.
https://developers.google.com/recaptcha/docs/faq#im-getting-an-error-localhost-is-not-in-the-list-of-supported-domains.-what-should-i-do
I added "localhost" and it is working perfectly fine.
OR
For reCAPTCHA v2, use the following test keys. You will always get No CAPTCHA and all verification requests will pass.
Reference
谷歌最近停止允许默认允许本地主机(正如 @Artur Cesar De Melo 所提到的)。这是他们的常见问题解答:
1:为您的开发环境创建单独的密钥
2:将 127.0.0.1 添加到允许的域列表中
3:保存更改并允许最多 30 分钟让更改生效
Google has recently stopped allowing localhost being allowed by default (as touched upon by @Artur Cesar De Melo). This is under their FAQ:
1: Create a separate key for your development environment
2: Add 127.0.0.1 to the list of allowed domains
3: Save changes and allow up to 30 minutess for changes to take affect
转至您的 Google reCAPTCHA 网站域列表 (https://www.google .com/recaptcha/admin/site/{siteid}/settings),添加
LOCALHOST
。如果上述方法不起作用,请尝试添加
127.0.0.1
。To your domains list of Google reCAPTCHA websites (https://www.google.com/recaptcha/admin/site/{siteid}/settings), add
LOCALHOST
.If the above doesn't work, try adding
127.0.0.1
too.reCAPTCHA 无法在 localhost/ 上运行。
使用
127.0.0.1/
而不是localhost/
。reCAPTCHA will not work on
localhost/
.Use
127.0.0.1/
instead oflocalhost/
.在 https://www.google.com/recaptcha/admin< 注册您的网站/a>
添加 JavaScript 插件
添加 Google 提供的类和站点密钥
Register your website at https://www.google.com/recaptcha/admin
Add the JavaScript plugin
Add the class and your site key provided by Google
根据 Google 的 reCAPTCHA 文档
As per Google's reCAPTCHA documentation
localhost 现在可以工作了。但是,请记住,将 localhost 添加到域名列表后,最多需要 30 分钟才能生效(根据域名列表显示的帮助提示)。
localhost works now. However, remember that after adding localhost to list of domain names, it takes up to 30 minutes to take effect (according to the help tip shown against the domain names list).
如果您有旧密钥,则应重新创建 API 密钥。还要注意代理。
If you have an old key, you should recreate your API key. Also be aware of proxies.
您可以写“localhost”或“127.0.0.1”,但 URL 必须相同。
示例:Google Domains 添加 ->本地主机 URL => localhost/login.php
示例:Google Domains 添加 -> 127.0.0.1 网址 => 127.0.0.1/登录.php
You can write "localhost" or "127.0.0.1", but the URL must be the same.
Example: Google Domains Add -> localhost URL => localhost/login.php
Example: Google Domains Add -> 127.0.0.1 URL => 127.0.0.1/login.php
我最近正在创建一个涉及 reCAPTCHA v2 的网站,我需要在我的本地主机上进行测试自动化。我没有将任何 IP 地址或 localhost 添加到 reCAPTCHA 管理门户中。
请按照以下步骤
登录到 reCAPTCHA 管理站点。 屏幕截图
找到关键设置
单击高级设置
在域名验证下,取消选中验证 reCAPTCHA 解决方案的来源复选框。此选项用于验证来自上面列出的域之一的请求。
请注意,如果禁用,您在验证解决方案时需要检查服务器上的主机名。
我创建了一个新密钥,禁用了它,并使用该密钥在本地主机中进行测试。
这是实现评论 reCAPTCHA 的示例页面 。
I was recently working on creating a website involving reCAPTCHA v2 and I had a need to do test automation on my localhost. I did not add any IP address or localhost into the reCAPTCHA administration portal.
Follow the below steps
Log in to the reCAPTCHA administration site. Screenshot
Locate Key Settings
Click on advanced settings
Under Domain Name Validation, uncheck Verify the origin of reCAPTCHA solutions checkbox. This option is used to verifying requests coming from one of the above listed domains.
Please note that, if disabled, you are required to check the hostname on your server when verifying a solution.
I created a new key, disabled this, and used this key for testing in localhost.
This is a sample page which implements reCAPTCHA for comments.
截至 2021 年的今天,Google reCAPTCHA 企业版控制台包含一项验证检查,不允许将
localhost
添加到允许的域列表中。为了解决这个问题(显然仅用于开发/测试目的),请为我执行以下操作:在您的 hosts 文件 将您的域解析为本地主机
不是通过导航到本地主机来测试您的应用程序,而是通过导航到您的域来测试您的应用程序。
我的本地服务器/应用程序通常在 http://localhost:4000 上运行,因此导航到 http://whatever-your-domain-will-be.com:4000 成功了。
部署或测试完成后,很容易删除 hosts 文件条目,并且 reCAPTCHA 允许的域列表中没有残留的垃圾。
As of today in 2021, the Google reCAPTCHA Enterprise version console includes a validation check that does not allow addition of
localhost
to the list of allowed domains. To get around this (obviously only for development/testing purposes) doing the following works for me:Create an entry in your hosts file that resolves your domain to your localhost
Test your application not by navigating to localhost, but rather by navigating to your domain.
My local server/app would normally run at http://localhost:4000, so navigating to http://whatever-your-domain-will-be.com:4000 did the trick.
Once deployed or testing is complete it's easy enough to remove the hosts file entry and there is no lingering trash in the reCAPTCHA allowed domains list.
这可能会帮助所有在本地主机上遇到 reCAPTCHA 问题的用户。谷歌确实说“默认情况下,所有密钥都在‘localhost’(或‘127.0.0.1’)上工作”,但在本地主机上真正使用 reCAPTCHA 可能会导致问题。就我而言,我使用安全令牌解决了这个问题。
我在这里发布了一个 PHP 的工作解决方案。
This may be helping all the users having problems with reCAPTCHA on localhost. Google indeed says "By default, all keys work on 'localhost' (or '127.0.0.1')", but for real using reCAPTCHA on localhost may cause problems. In my case, I solved it using secure tokens.
I posted a working solution for PHP here.
出于测试目的,如果您想在 localhost 上测试包含 reCAPTCHA 的网页,然后通过管理控制台<在域列表中添加localhost /a> 2: https://www.google.com/recaptcha/admin。 *建议为本地主机创建单独的站点密钥。
管理控制台屏幕截图
验证码输出
For testing purposes, if you want to test your web page which includes reCAPTCHA on localhost, then add localhost in the domain list by the Admin Console 2: https://www.google.com/recaptcha/admin. *It is advised to create a separate site key for localhost.
Admin Console Screenshot
CAPTCHA Output
对我有用的方法是使用我的外部 IP 地址。
如果您不知道它是什么,只需谷歌搜索“我的 IP 是什么”,
然后使用您的 IP 地址并将其设置在您的域中作为验证码,它应该开始正常工作。
The way that worked for me, was to use my external IP address.
If you don't know what it is, just google "What's my IP"
Then use your IP address and set this in your domains for the captcha and it should start working OK.
如果您使用 Laravel 那么您应该重新启动服务器
并运行以下两个命令:
If you are using Laravel then you should restart your server
and run the following two commands:
我将在迁移之前停用 Recaptcha。不幸的是,我认为我必须经历一次全新的迁移才能绕过 Recaptcha。
I am going to deactivate Recaptcha before migration. Unfortunately, I think I have to go through a whole new migration just to get around the Recaptcha.
对我来说,它可以删除我的实际配置并创建一个新配置,添加如下所示的域:
For me, it worked deleting my actual configuration and creating a new one, adding domains like this:
我在 Laravel 网站上收到与 reCAPTCHA 相关的错误。我通过使用一些命令和 env 文件解决了这个问题,以前的答案也将有助于解决这个问题。
首先,检查 env 文件(在您的情况下,是具有 Google reCAPTCHA 密钥的文件)中的 Google reCAPTCHA 密钥。
其次,运行以下命令:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear< /code>
php artisan route:clear
它会解决你的问题。
I got an error related to reCAPTCHA on a Laravel website. I resolved it by using some commands and with an env file and previous answers will also help in this problem.
First of all, check the env file (in your case, the file which has the Google reCAPTCHA key) for the Google reCAPTCHA key.
Second, run these commands:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear
It will solve your problem.
快速解答
Quick answer