Super simple form Captcha using RainCaptcha

David Carr

Tutorials PHP & MySQL

RainCaptcha is simple to use CAPTCHA for forms, it helps prevent spam submissions by creating images on the fly with embedded text. It can generate an URL to display a CAPTCHA validation image served by the RainCaptcha service.

The class can also send a request to RainCaptcha API to verify if the text that the user entered matches the text in the CAPTCHA image.

RainCaptcha is a CAPTCHA class that does not require any image processing extensions (GD, ImageMagick, etc). CAPTCHA was developed to be readable by humans and resistant to OCR software. It generates black-and-white images with 5 distorted letters on them and noise. Its checking algorithm is case-insensitive.<

Rather then writing a full class to do the heavy lifting make use of the free PHP class already available at  http://raincaptcha.driversworld.us/pages/docs_php_wrapper

To use the class include it on the page then create a new instance, this can then be used to generate a captcha image using ->getImage()

<?php $rainCaptcha = new RainCaptcha(); ?>
<img id="captchaImage" src="<?php echo $rainCaptcha->getImage(); ?>" />
<input name="captcha" type="text" />
<button type="button" class='btn btn-danger' onclick="document.getElementById('captchaImage').src = 
'<?php echo $rainCaptcha->getImage(); ?>&morerandom=' + Math.floor(Math.random() * 10000);"><span class="icon icon-refresh"></span></button>

To check if a user's input matches the captcha

$rainCaptcha = new RainCaptcha();
if(!$rainCaptcha->checkAnswer($_POST['captcha'])){
    die('You have not passed the CAPTCHA test!');
}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.