PHP Multiple CAPTCHAs Code Sample (BotDetect v3.0; deprecated)

The PHP Multiple Captchas project shows how to have multiple BotDetect CAPTCHA protected pages within the same PHP website.

First Time Here?

Check the BotDetect PHP Captcha Quickstart for key integration steps.

As long as the CAPTCHA instances have different names ("Page1Captcha" and "Page2Captcha" in the sample), they can have completely separate settings and won't interfere with each other's validation.

Even if a user opens the same page in multiple browser tabs, each tab will properly validate the shown CAPTCHA code.

Downloaded Location

The multiple PHP Captchas code sample is included in the samples/php_multiple_captchas_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>Multiple BotDetect CAPTCHAs PHP 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>Multiple BotDetect CAPTCHAs PHP Sample</h1>

    <h2>Page 1</h2>

    <fieldset>
      <legend>PHP CAPTCHA validation</legend>
      <label for="CaptchaCode">Retype the characters from the picture:</label>

      <?php // Adding BotDetect Captcha to the page
        $Page1Captcha = new Captcha("Page1Captcha");
        $Page1Captcha->UserInputID = "CaptchaCode1";
        $Page1Captcha->ImageStyle = ImageStyle::Bullets2;
        $Page1Captcha->CodeStyle = CodeStyle::Numeric;
        $Page1Captcha->CodeLength = 6;
        echo $Page1Captcha->Html();
      ?>

      <div class="validationDiv">
        <input type="text" name="CaptchaCode1" id="CaptchaCode1" />
        <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 = $Page1Captcha->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>

    <p class="navigation">&bull;&nbsp;<span>Page 1</span>&nbsp;&nbsp;&nbsp;
      &nbsp;&bull;&nbsp;<a href="page2.php">Page 2</a></p>

  </form>
</body>
</html>

On the first page, the "Page1Captcha").

page2.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>Multiple BotDetect CAPTCHAs PHP 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>Multiple BotDetect CAPTCHAs PHP Sample</h1>
    
    <h2>Page 2</h2>
    
    <fieldset>
      <legend>PHP CAPTCHA validation</legend>
      <label for="CaptchaCode">Retype the characters from the picture:</label>
      
      <?php // Adding BotDetect Captcha to the page 
        $Page2Captcha = new Captcha("Page2Captcha");
        $Page2Captcha->UserInputID = "CaptchaCode2";
        $Page2Captcha->ImageStyle = ImageStyle::Strippy;
        $Page2Captcha->CodeStyle = CodeStyle::Alpha;
        $Page2Captcha->CodeLength = 4;
        echo $Page2Captcha->Html();
      ?>
      
      <div class="validationDiv">
        <input type="text" name="CaptchaCode2" id="CaptchaCode2" />
        <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 = $Page2Captcha->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>
    
    <p class="navigation">&bull;&nbsp;<a href="index.php">Page 1</a>&nbsp;
      &nbsp;&nbsp;&nbsp;&bull;&nbsp;<span>Page 2</span></p>
    
  </form>
</body>
</html>

On the second page, we use a different Captcha identifier to distinguish between Captcha instances. This is important to do when you add Captcha protection to multiple forms in the same website (for example, one on the registration form and another in the comment form) because Captcha codes and other Captcha data are stored in Session state using keys based on CaptchaId values.

If multiple Captcha instances on different pages were using the same identifier, one Captcha's data would overwrite the other's when a visitor opened both forms simultaneously. This might not be a common concern since not many visitors will have multiple forms open at the same time in the same browser (and consequently, the PHP Session assigned), but it's best to avoid any possible problems and give each placed Captcha a unique identifier.


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.