- DelphiTools - https://www.delphitools.info -

Using reCAPTCHA in DWS WebServer

smallCaptchaSpaceWithRoughAlphaWe’ve all encoutered reCAPTCHA, as it’s one of the few effective ways to protect form submissions from bots on the internet.

Acquired by google in 2009, it comes with multiple plugins [1] for various web environment, here is a plugin for DWScript.

The plugin (aka unit) can be found in the DWSlibs [2] github repository, and it follows the naming conventions of the other reCAPTACH plugins, ie.

And here is a simple usage example. Obtain your reCAPTCHA API keys [3], save the sample code as “recap.dws” (replace with your API keys where appropriate), and open it in the browser.

Once you validate the captcha, it will display the outcome and offer a new captcha.

<html>
<body>
<pre><?pas
uses Security.reCaptcha;

if WebRequest.ContentField['recaptcha_challenge_field'] <> '' then begin
   PrintLn('Previous reCaptcha results:');
   var r := ReCaptcha.CheckAnswer('YourPrivateKey');
   PrintLn(r.IsValid);
   PrintLn(r.Error);
end;
?></pre>
<form action="recap.dws" method="post">
  <?pas= ReCaptcha.GetHtmlCode('YourPublicKey') ?>
</form>
</body>
</html>

The CheckAnswer overload used here is the short one, which assumes it is running for the POST with standard reCAPTCHA field names, so it grabs them and the remote IP automatically.