BotDetect Simple CAPTCHA Options: Server Side Persistence in Memcached Database Code Example

The BotDetect PHP Simple Captcha options: Server side persistence in Memcached database code example shows how to use Memcached to store persist Captcha data instead of using default BotDetect Sqlite Database persistence provider.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

As you may know Memcached is an open source, in-memory data structure store used to improve data performance. Therefore Captcha data storage via Memcached is neccessary in real world. This combination makes your application have better performance.

To use Memcached to store persist Captcha data, BotDetect Php Simple Captcha provides you a simple way by declaring Memcached configuration info in lib/config/botdetect.xml file, such as: memcached host, port.

BotDetect PHP Simple Captcha uses Memcached as Memcached php client, so you need to ensure Memcached is installed.

Downloaded Location

The PHP Captcha options: Server side persistence in Memcached database code example is included in the examples/simple-api-captcha-server-side-persistence-in-memcached-database-example folder of the download package.

Download the BotDetect PHP CAPTCHA Library and run this example

lib/config/botdetect.xml

<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="https://captcha.com/schema/php"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://captcha.com/schema/php
           https://captcha.com/schema/php/botdetect-4.2.2.xsd">

    <captchaStyles>
        <captchaStyle>
            <name>MemcachedCaptchaExample</name>
            <userInputID>CaptchaCode</userInputID>
            <codeLength>3-5</codeLength>
        </captchaStyle>
    </captchaStyles>

    <captchaPersistence>
        <persistenceType>Memcached</persistenceType>
        <address>127.0.0.1:11211</address>
    </captchaPersistence>
</botdetect>

In config/botdetect.xml configuration file, we tell BotDetect to use Memcached as the BotDetect persistence provider instead of the default one (SQLite Database). You can find the detailed explanation about this at Captcha Memcached persistence provider documentation.

index.php

<?php require("lib/simple-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 Options: Server Side Persistence in Memcached Database Code Example</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link type="text/css" rel="Stylesheet" href="stylesheet.css" />
</head>
<body>
  <form method="post" action="" class="column" id="form1">

    <h1>BotDetect PHP CAPTCHA Options: <br /> Server Side Persistence in Memcached Database Code Example</h1>

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

      <?php
        if (class_exists('Memcached')) {
          // Adding BotDetect Captcha to the page
          $MemcachedCaptchaExample = new SimpleCaptcha("MemcachedCaptchaExample");
          echo $MemcachedCaptchaExample->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 = $MemcachedCaptchaExample->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>
      <?php
        } else {
            echo "<div class='note warning'>Class Not Found: Memcached. You need to install Memcached first</div>";
        }
      ?>
    </fieldset>
  </form>
</body>
</html>

After including the BotDetect Captcha library, adding Captcha protection to the form is as simple as creating a SimpleCaptcha object instance and printing the Html function result. The same object instance provides easy Captcha validation using the Validate function.


Current BotDetect Versions