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.

index.php

<?php session_start(); ?>
<?php require("botdetect.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <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() ?>" />
</head>
<body>
  <form method="post" action="" id="form1">

    <h1>BotDetect PHP CAPTCHA Basic Sample</h1>
    
    <fieldset>
      <legend>PHP CAPTCHA validation</legend>
      <label for="CaptchaCode">Retype the characters from the picture:
      </label>
      
      <?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>";
            } 
          }
        ?>
      </div>
    </fieldset>
  </form>
</body>
</html>

After including the BotDetect Captcha library, adding Captcha protection to the form is as simple as creating a 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.