PHP Generate Custom Captcha Example

Hello,

Right this moment, i we’ll present you generate customized captcha in php. This text provides you with easy instance of generate customized captcha in php. you’ll be taught PHP generate customized captcha instance. So let’s comply with few step to create instance of generate customized captcha in php.

  • Index.php
  • fashion.css
  • contact_form.php
  • captcha.php

PHP Generate Custom Captcha Example

Index.php


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>PHP Generate Customized Captcha Instance </title>
  <hyperlink rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <hyperlink rel="stylesheet" href="belongings/css/fashion.css">
</head>
<physique>
  <div class="container mt-5">
    <?php embrace('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']; ?>
        </div>
      </div>
    <?php }?>
	<h2>PHP Generate Customized Captcha Instance </h2>
    <type motion="" identify="contactForm" technique="publish" enctype="multipart/form-data">
      <div class="form-group">
        <label>Title</label>
        <enter sort="textual content" class="form-control" identify="identify" id="identify">
      </div>

      <div class="form-group">
        <label>Electronic mail</label>
        <enter sort="electronic mail" class="form-control" identify="electronic mail" id="electronic mail">
      </div>

      <div class="row">
        <div class="form-group col-6">
          <label>Enter Captcha</label>
          <enter sort="textual content" class="form-control" identify="captcha" id="captcha">
        </div>
        <div class="form-group col-6">
          <label>Captcha Code</label>
          <img src="captcha.php" alt="PHP Captcha">
        </div>
      </div>

      <enter sort="submit" identify="ship" worth="Ship" class="btn btn-dark btn-block">
    </type>
  </div>
</physique>
</html>

belongings/css/fashion.css


.container {
  max-width: 700px;
  margin: 50px auto;
  text-align: left;
  font-family: sans-serif;
}
type {
  border: 1px stable #1A33FF;
  background: #007bff;
  padding: 40px 50px 45px;
}
.form-control:focus {
  border-color: #000;
  box-shadow: none;
}
label {
  font-weight: 600;
  shade:#fff;
}
.error {
  shade: purple;
  font-weight: 400;
  show: block;
  padding: 6px 0;
  font-size: 14px;
}
.form-control.error {
  border-color: purple;
  padding: .375rem .75rem;
}

contact_form.php


<?php
    session_start();
    
    if(!empty($_POST["send"])) {
      $identify = $_POST["name"];
      $electronic mail = $_POST["email"];
      $captcha = $_POST["captcha"];

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

      if(empty($captcha)) {
        $captchaError = array(
          "standing" => "alert-danger",
          "message" => "Please enter the captcha."
        );
      }
      else if($_SESSION['CAPTCHA_CODE'] == $captchaUser){
        $captchaError = array(
          "standing" => "alert-success",
          "message" => "Your type has been submitted successfuly."
        );
      } else {
        $captchaError = array(
          "standing" => "alert-danger",
          "message" => "Captcha is invalid."
        );
      }
    }  
?>

captcha.php


<?php
  session_start();

  // 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 picture
  $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 material-type: picture/jpeg");
  imagejpeg($layer);

?>

I hope it should help you…

Komentar

Postingan populer dari blog ini

Laravel 8 Get HTTP Hostname

PHP 8 Multiple Select Dropdown Example

JQuery Drag And Drop Menu Example