PHP Basic CAPTCHA Code Sample (BotDetect v3.0; deprecated)

First Time Here?

Check the BotDetect PHP Captcha Quickstart for key integration steps.

The PHP Basic Captcha code sample shows the most basic source code required to protect a PHP form with BotDetect CAPTCHA and validate the user input.

It can be used as a starting point when you are first learning how to use BotDetect.

Downloaded Location

The PHP basic Captcha code sample is included in the samples/php_basic_captcha_sample folder of the download package.


<?php session_start(); ?>
<?php require("botdetect.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 

<html xmlns="" >
  <title>BotDetect PHP CAPTCHA Basic Sample</title>
  <link type="text/css" rel="Stylesheet" href="StyleSheet.css" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link type="text/css" rel="Stylesheet" 
    href="<?php echo CaptchaUrls::LayoutStylesheetUrl() ?>" />
  <form method="post" action="" id="form1">

    <h1>BotDetect PHP CAPTCHA Basic Sample</h1>
      <legend>PHP CAPTCHA validation</legend>
      <label for="CaptchaCode">Retype the characters from the picture:
      <?php // Adding BotDetect Captcha to the page 
        $SampleCaptcha = new Captcha("SampleCaptcha");
        $SampleCaptcha->UserInputID = "CaptchaCode";
        echo $SampleCaptcha->Html(); 
      <div class="validationDiv">
        <input name="CaptchaCode" type="text" id="CaptchaCode" />
        <input type="submit" name="ValidateCaptchaButton" 
          value="Validate" id="ValidateCaptchaButton" />
        <?php // when the form is submitted
          if ($_POST) { 
            // validate the Captcha to check we're not dealing with a bot
            $isHuman = $SampleCaptcha->Validate();
            if (!$isHuman) {
              // Captcha validation failed, show error message
              echo "<span class=\"incorrect\">Incorrect code</span>";
            } else {
              // Captcha validation passed, perform protected action
              echo "<span class=\"correct\">Correct code</span>";

After Captcha object instance and printing the Html function result. The same object instance provides easy Captcha validation using the Validate function.

Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v3.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v4) is BotDetect v4 Captcha documentation index.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v4.0 page.