Sunday, April 5, 2009

Programming Re-captcha with Rebol

Captcha is a good system to prevent automated submission of forms by spammers. There is a free web-service from http://www.recaptcha.net that allows anyone to create a recaptcha form and use it for your own forms. It is quite simple actually.

You will definitely need to read this http://recaptcha.net/apidocs/captcha/client.html It explains how to embed the Recaptcha form within your own form on your own site.

If you want to get under the hood then read http://recaptcha.net/apidocs/captcha/ It will explain how to write your own Recaptcha processor. You may skip that if you use the Rebol system I've developed.

I program a lot in Rebol. However, there is no Recaptcha library specifically for Rebol. Turned out that it was quite easy to write one. The following piece of code is what you need in Rebol. I cannot get into the details of how Rebol works. Read docs at http://www.rebol.com for that.

In the following piece of code, it is assumed that the variable 'vars' is a bound object containing the values of the form being processed. If you insert the standard recaptcha into your form, then you should get vars/recpatcha_challenge_field and vars/recaptcha_response_field in that object. If you do not know how to do that, then simply use this script http://www.rebol.org/view-script.r?script=safe-cgi-data-read.r

This also assumes that your site is working on an Apache server. The REMOTE_ADDR environment variable is set on Apache servers. I am not so sure about other servers. Replace {YOURPRIVATEKEY} with whatever they sent you when you registered your site with recaptcha.net The public and private key is specific to your website.


Lastip: get-env "REMOTE_ADDR"

chall: replace/all vars/recaptcha_challenge_field " " "+"

resp: replace/all vars/recaptcha_response_field " " "+"

result: read/custom http://api-verify.recaptcha.net/verify

reduce ['POST rejoin ["privatekey={YOURPRIVATEKEY}&remoteip="
Lastip
"&challenge=" chall "&response=" resp
]


]

lns: load result

either lns/1 = "true" [

print [" Re-Captcha was correct!"]
;;;;at this point you may take the rest of the form
;;;;and proceed using it

] [

print ["Re-Captcha was not corret. Please try again"]

]

No comments:

Post a Comment