PHP Multiple CAPTCHAs Code Sample
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.
Download the BotDetect PHP CAPTCHA Library and run this sampleDownloaded 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">• <span>Page 1</span> • <a href="page2.php">Page 2</a></p> </form> </body> </html>
On the first page, the Captcha protection is added to the form using a Captcha identifier unique to the page ("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">• <a href="index.php">Page 1</a> • <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 consequentially, the PHP Session assigned), but it's best to avoid any possible problems and give each placed Captcha a unique identifier.
Please Note
BotDetect 3.0 PHP Captcha Library Beta1 is an in-progress port of BotDetect 3.0 Captcha, and we need you to guide our efforts towards a polished product. Please let us know if you encounter any bugs, implementation issues, or a usage scenario you would like to discuss.
BotDetect 3 PHP is already available for purchase at a 33% discounted pre-release price.
Current BotDetect Versions
- BotDetect PHP CAPTCHA v3.0.Beta12013 May 20
- BotDetect ASP.NET CAPTCHA v3.0.142013 May 20
- BotDetect ASP Classic CAPTCHA v3.0.142013 May 20



