将 reCAPTCHA 与传统 ASP 配合使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
重要提示:1.0 版 reCAPTCHA API 不再受支持,请升级到 2.0 版。了解详情
reCAPTCHA ASP 指南提供了一种为 ASP 添加人机识别系统的简单方法
页面,帮助您阻止漫游器滥用它。以下代码封装了 reCAPTCHA API。
注册 API 密钥后,您可以将 reCAPTCHA 添加到传统 ASP 网站,方法是将以下代码粘贴到
ASP 页面顶部:
<%
recaptcha_challenge_field = Request("recaptcha_challenge_field")
recaptcha_response_field = Request("recaptcha_response_field")
recaptcha_public_key = "your_public_key" ' your public key
recaptcha_private_key = "your_private_key" ' your private key
' returns the HTML for the widget
function recaptcha_challenge_writer()
recaptcha_challenge_writer = _
"<script type=""text/javascript"">" & _
"var RecaptchaOptions = {" & _
" theme : 'red'," & _
" tabindex : 0" & _
"};" & _
"</script>" & _
"<script type=""text/javascript"" src=""http://www.google.com/recaptcha/api/challenge?k=" & recaptcha_public_key & """></script>" & _
"<noscript>" & _
"<iframe src=""http://www.google.com/recaptcha/api/noscript?k=" & recaptcha_public_key & """ frameborder=""1""></iframe><>" & _
"<textarea name=""recaptcha_challenge_field"" rows=""3"" cols=""40""></textarea>" & _
"<input type=""hidden"" name=""recaptcha_response_field""value=""manual_challenge"">" & _
"</noscript>"
end function
' returns "" if correct, otherwise it returns the error response
function recaptcha_confirm(rechallenge,reresponse)
Dim VarString
VarString = _
"privatekey=" & recaptcha_private_key & _
"&remoteip=" & Request.ServerVariables("REMOTE_ADDR") & _
"&challenge=" & rechallenge & _
"&response=" & reresponse
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", "https://www.google.com/recaptcha/api/verify", False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send VarString
Dim ResponseString
ResponseString = split(objXmlHttp.responseText, vblf)
Set objXmlHttp = Nothing
if ResponseString(0) = "true" then
'They answered correctly
recaptcha_confirm = ""
else
'They answered incorrectly
recaptcha_confirm = ResponseString(1)
end if
end function
server_response = ""
newCaptcha = True
if (recaptcha_challenge_field <> "" or recaptcha_response_field <> "") then
server_response = recaptcha_confirm(recaptcha_challenge_field, recaptcha_response_field)
newCaptcha = False
end if
%>
此时,每当调用server_response
newCaptcha
以便确定网页的状态。
您可以使用以下 HTML 作为框架:
<html>
<body>
<% if server_response <> "" or newCaptcha then %>
<% if newCaptcha = False then %>
<!-- An error occurred -->
Wrong!
<% end if %>
<!-- Generating the form -->
<form action="recaptcha.asp" method="post">
<%=recaptcha_challenge_writer()%>
</form>
<% else %>
<!-- The solution was correct -->
Correct!
<%end if%>
</body>
</html>
延伸阅读
自定义外观和风格
提示和准则
问题排查
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003ereCAPTCHA v1.0 is deprecated; upgrade to v2.0 for continued support.\u003c/p\u003e\n"],["\u003cp\u003eThis guide demonstrates how to integrate reCAPTCHA into classic ASP websites to prevent bot abuse.\u003c/p\u003e\n"],["\u003cp\u003eProvided ASP code snippets enable rendering the reCAPTCHA widget and validating user responses.\u003c/p\u003e\n"],["\u003cp\u003eSample HTML illustrates how to display the reCAPTCHA challenge and handle success/failure scenarios within your ASP page.\u003c/p\u003e\n"]]],["This content outlines how to implement reCAPTCHA on a classic ASP site to prevent bot abuse. Key actions include obtaining API keys, pasting provided code at the top of an ASP page, and using `recaptcha_challenge_writer()` to display the CAPTCHA widget. The `recaptcha_confirm()` function verifies the user's CAPTCHA response. Variables `server_response` and `newCaptcha` are set to manage page state, determining if there is a new CAPTCHA or if the response was wrong. The provided HTML skeleton determines to generate a new CAPTCHA or show a confirmation message.\n"],null,["# Using reCAPTCHA with Classic ASP\n\n**Important** : Version 1.0 of the reCAPTCHA API is no longer supported, please upgrade to Version 2.0. [Learn more](/recaptcha/docs/faq)\n\nThe reCAPTCHA ASP guide provides a simple way to place a [CAPTCHA](http://www.google.com/recaptcha/) on your ASP\npage, helping you stop bots from abusing it. The code below wraps the [reCAPTCHA API](/recaptcha/old/intro).\n\nAfter you've signed up for your API keys, you can add reCAPTCHA to your classic ASP site by pasting the code below at the\ntop of your ASP page: \n\n```\n \u003c%\n recaptcha_challenge_field = Request(\"recaptcha_challenge_field\")\n recaptcha_response_field = Request(\"recaptcha_response_field\")\n recaptcha_public_key = \"your_public_key\" ' your public key\n recaptcha_private_key = \"your_private_key\" ' your private key\n\n ' returns the HTML for the widget\n function recaptcha_challenge_writer()\n\n recaptcha_challenge_writer = _\n \"\u003cscript type=\"\"text/javascript\"\"\u003e\" & _\n \"var RecaptchaOptions = {\" & _\n \" theme : 'red',\" & _\n \" tabindex : 0\" & _\n \"};\" & _\n \"\u003c/script\u003e\" & _\n \"\u003cscript type=\"\"text/javascript\"\" src=\"\"http://www.google.com/recaptcha/api/challenge?k=\" & recaptcha_public_key & \"\"\"\u003e\u003c/script\u003e\" & _\n \"\u003cnoscript\u003e\" & _\n \"\u003ciframe src=\"\"http://www.google.com/recaptcha/api/noscript?k=\" & recaptcha_public_key & \"\"\" frameborder=\"\"1\"\"\u003e\u003c/iframe\u003e\u003c\u003e\" & _\n \"\u003ctextarea name=\"\"recaptcha_challenge_field\"\" rows=\"\"3\"\" cols=\"\"40\"\"\u003e\u003c/textarea\u003e\" & _\n \"\u003cinput type=\"\"hidden\"\" name=\"\"recaptcha_response_field\"\"value=\"\"manual_challenge\"\"\u003e\" & _\n \"\u003c/noscript\u003e\"\n\n end function\n\n ' returns \"\" if correct, otherwise it returns the error response\n function recaptcha_confirm(rechallenge,reresponse)\n\n Dim VarString\n VarString = _\n \"privatekey=\" & recaptcha_private_key & _\n \"&remoteip=\" & Request.ServerVariables(\"REMOTE_ADDR\") & _\n \"&challenge=\" & rechallenge & _\n \"&response=\" & reresponse\n\n Dim objXmlHttp\n Set objXmlHttp = Server.CreateObject(\"Msxml2.ServerXMLHTTP\")\n objXmlHttp.open \"POST\", \"https://www.google.com/recaptcha/api/verify\", False\n objXmlHttp.setRequestHeader \"Content-Type\", \"application/x-www-form-urlencoded\"\n objXmlHttp.send VarString\n\n Dim ResponseString\n ResponseString = split(objXmlHttp.responseText, vblf)\n Set objXmlHttp = Nothing\n\n if ResponseString(0) = \"true\" then\n 'They answered correctly\n recaptcha_confirm = \"\"\n else\n 'They answered incorrectly\n recaptcha_confirm = ResponseString(1)\n end if\n\n end function\n\n server_response = \"\"\n newCaptcha = True\n if (recaptcha_challenge_field \u003c\u003e \"\" or recaptcha_response_field \u003c\u003e \"\") then\n server_response = recaptcha_confirm(recaptcha_challenge_field, recaptcha_response_field)\n newCaptcha = False\n end if\n\n %\u003e\n```\n\nWhat happens here is the variables `server_response` and `newCaptcha` are set whenever the\npage is loaded, allowing you to determine the state of your page.\n\nYou can use the following HTML as a skeleton: \n\n```swig\n \u003chtml\u003e\n \u003cbody\u003e\n\n \u003c% if server_response \u003c\u003e \"\" or newCaptcha then %\u003e\n\n \u003c% if newCaptcha = False then %\u003e\n\n \u003c!-- An error occurred --\u003e\n Wrong!\n\n \u003c% end if %\u003e\n\n \u003c!-- Generating the form --\u003e\n \u003cform action=\"recaptcha.asp\" method=\"post\"\u003e\n \u003c%=recaptcha_challenge_writer()%\u003e\n \u003c/form\u003e\n\n \u003c% else %\u003e\n\n \u003c!-- The solution was correct --\u003e\n Correct!\n\n \u003c%end if%\u003e\n\n \u003c/body\u003e\n \u003c/html\u003e\n```\n\n### Further Reading\n\n- [Customizing Look and Feel](/recaptcha/old/docs/customization)\n- [Tips and Guidelines](/recaptcha/old/docs/tips)\n- [Troubleshooting](/recaptcha/old/docs/troubleshooting)"]]