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

The BotDetect PHP Simple Captcha options: Server side persistence in Redis database code example shows how to use Redis 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 Redis is an open source, in-memory data structure store used to improve data performance. Therefore Captcha data storage via Redis is neccessary in real world. This combination makes your application have better performance.

To use Redis to store persist Captcha data, BotDetect PHP Simple Captcha provides you a simple way by declaring some Redis configuration info in lib/config/botdetect.xml file, such as: redis host, port, password, etc.

BotDetect PHP Simple Captcha uses Predis as Redis PHP client, which is already included in our library. You also need to ensure that the Redis server is running.

Downloaded Location

The PHP Captcha options: Server side persistence in Redis database code example is included in the examples/simple-api-captcha-server-side-persistence-in-redis-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>RedisCaptchaExample</name>
            <userInputID>CaptchaCode</userInputID>
            <codeLength>3-5</codeLength>
        </captchaStyle>
    </captchaStyles>

    <captchaPersistence>
        <persistenceType>Redis</persistenceType>
        <host>localhost</host>
        <port>6379</port>
        <timeout>2000</timeout>
        <password></password>
    </captchaPersistence>
</botdetect>

In lib/botdetect.xml configuration file, we tell BotDetect to use Redis as the BotDetect persistence provider instead of the default one (SQLite Database). You can find the detailed explanation about this at Captcha Redis 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 Redis 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 Redis Database Code Example</h1>

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

      <?php
        // Adding BotDetect Captcha to the page
        $RedisCaptchaExample = new SimpleCaptcha("RedisCaptchaExample");
        echo $RedisCaptchaExample->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 = $RedisCaptchaExample->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 SimpleCaptcha object instance and printing the Html function result. The same object instance provides easy Captcha validation using the Validate function.


Current BotDetect Versions