Implement ReCaptcha in classic ASP

414 Views Asked by At

I've got a problem implementing ReCaptcha from google in classic asp. The Captcha displays well below my form, but when I submit the form, it's always accepted even if the captcha fields are left empty or are wrong.

Here's the code I use in my first "form" page. Right at the top of the page I've this :

    <%
  recaptcha_challenge_field  = Request("recaptcha_challenge_field")
  recaptcha_response_field   = Request("recaptcha_response_field")
  recaptcha_public_key       = "mykeyofcourse" ' your public key
  recaptcha_private_key      = "and here the private one" ' 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"">" & _

""

  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

   %>

in my form, above the submit button I have this code :

  <%=recaptcha_challenge_writer()%>

Now to the ASP page that checks the form. On the top of the page I have this :

if server_response <> "" or newCaptcha then Erreur="oui" else Erreur="non" end if

response.write "erreur : "&erreur

But...erreur is always = non whatever the value of the captcha field is. Any idea what I'm doing wrong

Thanks in advance

0

There are 0 best solutions below