How To Generate Custom Captcha Code In PHP Example




How To Generate Custom Captcha Code In PHP Example

Hi guys,

Today i will explained How To create a custom captcha code generate in php. This example is so easy to use in php. This example to perform A generate form captcha code in manually.

This example to i am create a few files so follow my steps. I am create a asset/css/style.css file and script/captcha.php and script/contact_form.php directory folder inside.

So let's start to the example.


<!DOCTYPE html>

<html lang="en">


<meta charset="utf-8">

<title>How To Generate Custom Captcha Code In PHP Example -</title>

<link rel="stylesheet" href="">

<link rel="stylesheet" href="assets/css/style.css">



<div class="container mt-5">

<?php include('scripts/contact_form.php'); ?>

<?php if(!empty($captchaError)) {?>

<div class="form-group col-12 text-center">

<div class="alert text-center <?php echo $captchaError['status']; ?>">

<?php echo $captchaError['message']; ?>



<?php }?>

<form action="" name="contactForm" method="post" enctype="multipart/form-data">

<div class="form-group">


<input type="text" class="form-control" name="name" id="name">


<div class="form-group">


<input type="email" class="form-control" name="email" id="email">


<div class="row">

<div class="form-group col-6">

<label>Enter Captcha</label>

<input type="text" class="form-control" name="captcha" id="captcha">


<div class="form-group col-6">

<label>Captcha Code</label>

<img src="scripts/captcha.php" alt="PHP Captcha">



<input type="submit" name="send" value="Send" class="btn btn-dark btn-block">






.container {

max-width: 500px;

margin: 50px auto;

text-align: left;

font-family: sans-serif;


form {

border: 1px solid #1A33FF;

background: #ecf5fc;

padding: 40px 50px 45px;


.form-control:focus {

border-color: #000;

box-shadow: none;


label {

font-weight: 600;


.error {

color: red;

font-weight: 400;

display: block;

padding: 6px 0;

font-size: 14px;


.form-control.error {

border-color: red;

padding: .375rem .75rem;





if(!empty($_POST["send"])) {

$name = $_POST["name"];

$email = $_POST["email"];

$captcha = $_POST["captcha"];

$captchaUser = filter_var($_POST["captcha"], FILTER_SANITIZE_STRING);

if(empty($captcha)) {

$captchaError = array(

"status" => "alert-danger",

"message" => "Please enter the captcha."



else if($_SESSION['CAPTCHA_CODE'] == $captchaUser){

$captchaError = array(

"status" => "alert-success",

"message" => "Your form has been submitted successfuly."


} else {

$captchaError = array(

"status" => "alert-danger",

"message" => "Captcha is invalid."








// Generate captcha code

$random_num = md5(random_bytes(64));

$captcha_code = substr($random_num, 0, 6);

// Assign captcha in session

$_SESSION['CAPTCHA_CODE'] = $captcha_code;

// Create captcha image

$layer = imagecreatetruecolor(168, 37);

$captcha_bg = imagecolorallocate($layer, 247, 174, 71);

imagefill($layer, 0, 0, $captcha_bg);

$captcha_text_color = imagecolorallocate($layer, 0, 0, 0);

imagestring($layer, 5, 55, 10, $captcha_code, $captcha_text_color);

header("Content-type: image/jpeg");



Now you can check your own.

I hope it can help you...

#PHP 8