4 Steps to Create CAPTCHA Script in PHP

This is a powerful CAPTCHA script, using this script I’m going to generate a random number and then creates an image. If the user submits the form with correct captcha value, then you can proceed with the next statements like inserting data into the database or sending an email.

Some Steps to Add “No CAPTCHA reCAPTCHA” Using PHP


1- Create HTML Page with Basic HTML Markup

This is Basic HTML Markup with simple form inside body section with name, email & captcha fields also a submit button. Save this file as index.php

2- Create a captcha.php file to Generate Captcha (CAPTCHA Script in PHP)

Create a new PHP file and save it as captcha.php, then open a PHP block and write session_start PHP function.

Then generate a random number using rand function and store this in the session variable. Below is the example code.

session_start();
$captcha_num = rand(1000, 9999);
$_SESSION[‘code’] = $captcha_num;

You can use any above code to generate captcha as per your requirement.

Define sizes of captcha image and also font size of captcha.
$font_size = 30;
$img_width = 70;
$img_height = 40;

Then define content type of cpatcha.php file to image/jpeg
header(‘Content-type: image/jpeg’);

Create captcha image with defined sizes and set background color, you can use this below code.

$image = imagecreate($img_width, $img_height); // create background image with dimensions
imagecolorallocate($image, 255, 255, 255); // set background color

$image = imagecreate($img_width, $img_height); // create background image with dimensions
imagecolorallocate($image, 255, 255, 255); // set background color
Set captcha text color, you can play with these values and settle with your desired colors.

$text_color = imagecolorallocate($image, 0, 0, 0); // set captcha text color

$text_color = imagecolorallocate($image, 0, 0, 0); // set captcha text color
Create captcha image from generated captcha string. And then output this image in browser. We are generating captcha text from a specific font, you can use font whatever you like.

imagettftext($image, $font_size, 0, 15, 30, $text_color, ‘font.ttf’, $captcha_num);
imagejpeg($image);

3- Display this captcha code in our Form


In our first steps, I’ve added a simple form with a captcha input field. To display captcha code use image tag and in the source, attribute add captcha.php file. After adding your code should look like this below code.

XHTML
Simple CAPTCHA Script in PHP-

4- Check the captcha code after form submission

Next, we need to verify the submitted captcha code is correct or not. We can do this by checking the submitted captcha value with session value if both values match then submitted captcha value is true and if both values are not matching then the submitted value is not correct.

User session_start PHP function. And check if POST superglobal is set and not empty. If the condition is true then check submitted captcha code & code stored in session superglobal. Below is the code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »