需要在.asp页面中实现recaptcha

时间:2014-09-23 02:29:15

标签: asp.net asp-classic recaptcha

任何人都应该知道如何在.asp中添加recaptcha,我有公共密钥和私密密钥

              <TD align="middle">Security Code: </TD>
              <TD align="middle">
                <script type="text/javascript"
                   src="http://www.google.com/recaptcha/api/challenge?k=01M2eMS32Xs6oGtCaBu3AkHQ==">
                </script>
                <noscript>
                   <iframe src="http://www.google.com/recaptcha/api/noscript?k=01M2eMS32Xs6oGtCaBu3AkHQ=="
                       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>

              </TD>

1 个答案:

答案 0 :(得分:1)

将reCAPTCHA与Classic ASP一起使用

reCAPTCHA ASP指南提供了一种在您的ASP页面上放置CAPTCHA的简单方法,可帮助您阻止机器人滥用它。下面的代码包含了reCAPTCHA API。

在您注册了API密钥之后,您可以通过粘贴ASP页面顶部的以下代码将reCAPTCHA添加到您的经典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", "http://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>

如下所示: http://developers.google.com/recaptcha/docs/asp