How to Generate Captcha Image in PHP ?

What Does Captcha Stand For?

A Captcha is a type of challenge-response test used in the website as an attempt to ensure that the response is generated by a human being. A Captcha is one kind of program that can generate and also test that humans can pass but computer programs cannot. Say, for example, humans can read distorted text, but current computer programs can't:

CAPTCHA means "Completely Automated Public Turing test to tell Computers and Humans Apart".

Types of Captcha

There are basically 3 types of captcha. They are as below:

  • Image Captcha
  • Mathematical Captcha
  • Voice Captcha

Uses of Captcha

  • To Prevent  Comment Spam in Blogs
  • Website Registration Protection
  • Online Polls
  • To  Prevent Dictionary Attacks
  • Search Engine Bots
  • Email Worms and Spam

Among all the types as given above, image captcha is the most popular and widely used. So, In this tutorial, we will learn how to generate and use image captcha using PHP. This tutorial is divided into mainly three parts. We will see them one by one.


Image Captcha Generation

Here is the PHP code to generate images with random code. Save it as captcha.php.First start a session to store captcha code in session then generate random code using the PHP rand() function and write it on the generated image.

$ResultStr = '';
for ($i = 0; $i < 5; $i++) {
    $ResultStr .= chr(rand(97, 122));

$NewImage  = imagecreatefromjpeg("img.jpg");
$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 127, 127, 127);
imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor); 
$_SESSION['key'] = $ResultStr;
header("Content-type: image/jpeg");


Image Captcha Integration in HTML

Save the below file as index.php.

  <form name="post" method="post" action="post.php">
    <div class="captcha_example">
        <div class="top"><p>Captcha generation in PHP | PHP Dev Zone</p></div>
        <div class="inner">
                <div style="margin-bottom:10px;">
                    <h4>Captcha image:</h4>
                    <img src="captcha.php" class="form_captcha" />
                    <div class="lines">Verification (Type what you see):</div>
                    <input name="number" type="text" class="captcha">
                    <input name="Submit" type="submit" class="main-color-bg" value="Submit">
                <p>Here  captcha image is being generated using GD library, each time when page loads it display different combinations of symbols.</p>
                <h2 style="float: right;"><a href="">Back to Article</a></h2>


Image Captcha Response Validation

First, start a session to access $_SESSION[“key”] variable then compare it with posted captcha response. If both are the same then run your code else exit from code with message Wrong Code Entered. Save below file as post.php

if (isset($_REQUEST['Submit'])) {
  $number = $_REQUEST['number'];   
  $key    = $_SESSION['key'];
  if ($number!=$key) {
      echo ' Wrong Code Entered! Please try again!';
  } else {
      echo ' Your string is valid!';
      //You can make query to insert the data into database here.


That’s it. Simple Captcha Generation in PHP is Completed.


Ketan Patel

As a backend and ecommerce developer, I have extensive experience in implementing robust and scalable solutions for ecommerce websites and applications. I have a deep understanding of server-side technologies and have worked with various programming languages. I have experience in integrating with payment gateways, and other third-party services.