Captcha is very important for form validation and other related type. This tutorial teaches you how to create and add a simple but powerful Anti-Spam Captcha to your system or application using PHP.

A good way to avoid automatic form submissions when creating a web form or to prevent automated web robot tools and spammers from taking advantage of your forms and scripts is to use some kind of verification process.

This is simple php code which create Captcha . First create a view file where form will be show

<script src=""></script>
<div class="col-sm-7">
    <input  type="text" placeholder="Enter Code" id="captcha"
           name="captcha"  value="" style="vertical-align: super;">
    <img src="http://localhost/captcha.php" class="imgcaptcha" alt="captcha" style="vertical-align: bottom"/>
    <img src="" alt="reload" class="refresh"/>

    $(".refresh").click(function () {
        $(".imgcaptcha").attr("src", "http://localhost/captcha.php?_=" + ((new Date()).getTime()));



After this create a php file which generate captcha.


function getRandomWord($len = 5) {
    $word = array_merge(range('0', '9'), range('A', 'Z'));
    return substr(implode($word), 0, $len);

$ranStr = !empty($_SESSION["vercode_signup"]) ? $_SESSION["vercode_signup"] : getRandomWord();
$_SESSION["vercode"] = $ranStr;

$height = 35; //CAPTCHA image height
$width = 150; //CAPTCHA image width
$font_size = 24; 

$image_p = imagecreate($width, $height);
$graybg = imagecolorallocate($image_p, 245, 245, 245);
$textcolor = imagecolorallocate($image_p, 34, 34, 34);

imagefttext($image_p, $font_size, -2, 15, 26, $textcolor, '', $ranStr);
//imagestring($image_p, $font_size, 5, 3, $ranStr, $white);


It will show captcha and when refresh then generate new code. If you face any problem then please inform me.