Integrate API

Integrate API - Header

Friday, July 10, 2015

How to integrate Simple Captcha

Captcha is one of powerful technique to prevent automated data submission in a form. All developers must agree that, it's the best and useful technique where user data input need to allow. for example in a signup form or login or even user comments. Although for some visitors it's annoying but no alternative at all. So the best practice is to use a simple captcha. Most popular captcha is re-captcha which is extremely complicated and not user friendly at all. So here i am going to show you how you can integrate a very simple captcha in few minutes.

A simple and good one will be if we are able to create a captcha using php GD library. Almost all recent linux hosted servers have PHP GD2 library support, so we don't need to rethink about it.

in your signup/login/other form:

<?php
session_start();
include("simple-php-captcha.php");
if(isset($_POST['submit']))
{
  if($_POST['captcha_text']==$_SESSION['captcha']['code'])
    echo "Correct";
  else
    echo "Incorrect";
}
$_SESSION['captcha'] = simple_php_captcha();
?>
download simple-php-captcha.php file from here.

Here is your sample form:
<form action="" method="post" enctype="multipart/form-data">
 Username: <input type="text" name="username" /><br />
 Password: <input type="password" name="password" /><br />
 <img src="<?php echo  $_SESSION['captcha']['image_src'];?>" /><br />
 Captcha: <input type="text" name="captcha_text" />
 <input type="submit" value="Submit" name="submit" />
</form>

Make sure backgrounds and fonts folders are in same path to simple-php-captcha.php file.

Happy Captcha !!!