Join WhatsApp ChannelJoin Now

PHP Generate Custom Captcha Example


Today, i we will show you how to generate custom captcha in php. This article will give you simple example of how to generate custom captcha in php. you will learn PHP generate custom captcha example. So let’s follow few step to create example of how to generate custom captcha in php.

  • Index.php
  • style.css
  • contact_form.php
  • captcha.php

PHP Generate Custom Captcha Example


<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <title>PHP Generate Custom Captcha Example </title>
  <link rel="stylesheet" href="">
  <link rel="stylesheet" href="assets/css/style.css">
  <div class="container mt-5">
    <?php include('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 }?>
	<h2>PHP Generate Custom Captcha Example </h2>
    <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="captcha.php" alt="PHP Captcha">

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


.container {
  max-width: 700px;
  margin: 50px auto;
  text-align: left;
  font-family: sans-serif;
form {
  border: 1px solid #1A33FF;
  background: #007bff;
  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");


I hope it will assist you…

Recommended Posts