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="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></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="http://techparkbd.com/subdomain/source_file/refresh.png" alt="reload" class="refresh"/>
</div>

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

    });

</script>

After this create a php file which generate captcha.

<?php
	
session_start();

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

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

$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, 'http://techparkbd.com/subdomain/source_file/mono.ttf', $ranStr);
//imagestring($image_p, $font_size, 5, 3, $ranStr, $white);
imagepng($image_p);

	
?>

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