Simple and Small Captcha to Diminish the Vulnarability of your Website
Posted by Abzetdin Adamov on February 9, 2011
You can use one of Google solutions Recaptcha at http://www.google.com/recaptcha. But sometimes you can’t use this solution for some reasons:
- You may not want your server connect to any resources in Internet (as in my case)
- You may not want to use software of other party
- Google Recaptcha don’t offer the level of customization you need
- …
Because of the similar reasons I’ve decided to develop my own captcha. As a result you can see following code of very simple and small, but at the same time enough robust and flexible Captcha. You can define any length for Captcha string as well as the type of string: numbers, chars or combination of numbers and chars. It can be easily used just by following to the short instructions.
Include this code just before proceeding the data you get from form. It verify the captcha image with value entered to cahtcha input (reg_captch).
<?php session_start(); $captcha = $_POST["reg_captch"]; if (isset($captcha) && isset($_SESSION["captch"])){ if ($captcha == $_SESSION["captch"]){ // call appropriate function here or do nothing to continue to run the rest script } else { exit("Captcha error..."); } } else { exit("Captcha error..."); } ?>
Add the following HTML code into your Form (before Submit button may be the right place)
<input class=in_text type=text name=reg_captch id=reg_captch> <img id="captch" src="captcha.php?rnd=<?=rand();?>" width="100" height="> <!-- I'm using random values here to prevent image caching in browser, it's important for Firefox, Chrome, ... --> <img style="cursor:pointer" src="images/refresh.png" alt="Refresh" onClick="javascript:document.getElementById('captch').src='captcha.php?' + Math.random();">
Here you can get main code of captcha (captcha.php), as well as zip file with captcha background image (security_background.gif) and text font to display captcha value (feel free to use your own …)
Main Code – captcha.php
Resource Files – resources
Leave a Reply