PHP Captcha Tutorial Example

03-Apr-2023

.

Admin

PHP Captcha Tutorial Example

Hi Guys,

In this example,I will learn you how to create captcha in php contact form.you can easy and simply create captcha in php form.

Captcha is one of the best remedies for this hazard. It prevents anonymous access and stops robots from sending data.

This tutorial explains about PHP Captcha, and we will learn how to create a PHP Captcha Script using the GD library, also look at how to validate the captcha with server-side PHP. Finally, we will also learn how to implement the captcha in the contact form.

When allowing users to enter data into our website, we need to check whether the data is entered by the human. Otherwise, people will use robots to push the bulk of unwanted data into the website.

Step 1: HTML Contact Form with Captcha


<!DOCTYPE html>

<html>

<head>

<title>PHP Captcha with Contact Form Example</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

</head>

<style type="text/css">

.container {

max-width: 500px;

margin: 50px auto;

text-align: left;

font-family: sans-serif;

}

form{

border: 1px solid darkcyan !important;

background: #e6f3e5;

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;

}

</style>

<body>

<div class="container">

<div class="row">

<div clas="col-md-12">

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

<!-- Contact form -->

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

<div class="row">

<div class="col-md-12">

<!-- Captcha error message -->

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

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

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

</div>

<?php }?>

</div>

<div class="col-md-12">

<div class="form-group">

<label>Name</label>

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

</div>

</div>

<div class="col-md-12">

<div class="form-group">

<label>Email</label>

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

</div>

</div>

<div class="col-md-12">

<div class="row">

<div class="col-md-6">

<div class="form-group">

<label>Enter Captcha</label>

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

</div>

</div>

<div class="col-md-6">

<div class="form-group">

<label>Captcha Code</label><br>

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

</div>

</div>

</div>

</div>

<div class="col-md-12 mt-3">

<input type="submit" name="send" value="Submit" class="btn btn-success btn-block">

</div>

</div>

</form>

</div>

</div>

</div>

</body>

</html>

Step 2: Create PHP Captcha with GD Library

Set captcha code in the PHP session. Generate a random number with 6 characters. This will be out security code. Create a basic captcha using the GD library built-in functions. This code will generate the captcha image.

<?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 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");

imagejpeg($layer);

?>

Step 3: Add CAPTCHA to Contact Form

Add captcha with the contact form, and we get the captcha code from the session that we declared earlier. We have not validated the form, but we validated the captcha. When there is no captcha entered, the user will be notified with the required captcha message. When the user entered the wrong captcha user will also be alerted with a message.

<?php

session_start();

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."

);

}

}

?>

It will help you...

#PHP