Create Your Own CAPTCHA in PHP | PHP Tutorial For Beginners

This tutorial shows you how to build a simple PHP CAPTCHA image generator using PHP's GD library and integrate it into a feedback form.

Note: Create a fonts/ folder in your project root and copy arial.ttf into it (you can find it in your operating system's fonts folder).

Step 1 — Create captcha.php

This file generates a random 5-letter string, stores it in a session, and renders it as a PNG image.

<?php
session_start();

// Generate 5 random lowercase letters
$string = '';
for ($i = 0; $i < 5; $i++) {
    $string .= chr(rand(97, 122));
}
$_SESSION['captcha'] = $string;

$font_path      = 'fonts/';
$captcha_image  = imagecreatetruecolor(150, 60);
$text_color     = imagecolorallocate($captcha_image, 200, 100, 90);
$bg_color       = imagecolorallocate($captcha_image, 255, 255, 255);

imagefilledrectangle($captcha_image, 0, 0, 399, 99, $bg_color);
imagettftext($captcha_image, 30, 0, 10, 40,
             $text_color, $font_path . 'arial.ttf',
             $_SESSION['captcha']);

header("Content-type: image/png");
imagepng($captcha_image);
?>

Step 2 — Create feedback.php

The feedback form displays the CAPTCHA image and validates the user's input against the session value.

<?php
session_start();
if (isset($_POST['submit'])) {
    if (!empty($_POST['name']) && !empty($_POST['email'])
        && !empty($_POST['message']) && !empty($_POST['code'])) {
        if ($_POST['code'] == $_SESSION['captcha']) {
            // send email to admin here
            $accept = "Thank you for your feedback.";
        } else {
            $error = "Incorrect security code — please try again.";
        }
    } else {
        $error = "Please fill in all fields.";
    }
}
?>
<html>
<head>
  <title>Feedback Form</title>
  <style>
    .error  { color: red; }
    .accept { color: green; }
  </style>
</head>
<body>
  <?php if (!empty($error))  echo '<div class="error">'  . $error  . '</div>'; ?>
  <?php if (!empty($accept)) echo '<div class="accept">' . $accept . '</div>'; ?>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
      <tr><td colspan="2" align="center"><b>Feedback Form</b></td></tr>
      <tr>
        <td>Your Name:</td>
        <td><input type="text" name="name"></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><input type="text" name="email"></td>
      </tr>
      <tr>
        <td>Feedback:</td>
        <td><textarea name="message"></textarea></td>
      </tr>
      <tr>
        <td></td>
        <td><img src="captcha.php"></td>
      </tr>
      <tr>
        <td>Security Code:</td>
        <td><input type="text" name="code"></td>
      </tr>
      <tr>
        <td><input type="submit" name="submit" value="Submit"></td>
      </tr>
    </table>
  </form>
</body>
</html>

How It Works

  • captcha.php generates a random string, stores it in $_SESSION['captcha'], and outputs it as a PNG using imagettftext().
  • The form embeds the CAPTCHA image via <img src="captcha.php">.
  • On submit, the user's typed code is compared against $_SESSION['captcha']. A match means it's a human.

Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.

← Older Post Newer Post →