Example

The following example implements the standard use of a CAPTCHA: Submitted form data is only evaluated when a CAPTCHA has been solved correctly.

Creating a Numeral CAPTCHA

This example is just to show you how to generate a simple mathematic operation with Text_CAPTCHA_Numeral.

<?php
require_once 'Text/CAPTCHA/Numeral.php';
$num       = new Text_CAPTCHA_Numeral;
$operation $num->getOperation();

/**
 * This will print the mathematical operation
 * that has been generated by the package.
 */
print $operation;
?>

Securing a form with a Numeral CAPTCHA

This example will show you how to secure a form using the numeral captcha. It generates an operation and stores its result into a session variable.

<?php
require_once 'Text/CAPTCHA/Numeral.php';
$numcap = new Text_CAPTCHA_Numeral;

if (isset(
$_POST['captcha']) && isset($_SESSION['answer'])) {
    if (
$_POST['captcha'] == $_SESSION['answer']) {
        
$errors[] = 'Ok... You might be human...';
    } else {
        
$errors[] = 'You are dumb or not human';
    }
}
    if (!empty(
$errors)) {
        foreach (
$errors as $error) {
            print 
"<h1><font color='red'>$error</font></h1><br />";
        }
    }


    print 
'
        <form name="capter" action="index.php?page=liveExample" method="post">
         <table>
          <tr>
           <th>What is this result pilgrim?: '
.$numcap->getOperation().'</th>
           <td><input type="text" value="" name="captcha" /></td>
          </tr>
          <tr>
           <th/>
           <td><input type="submit" value="Let me prove you that I am human!" /></td>
          </tr>
        </form>
    '
;
    
$_SESSION['answer'] = $numcap->getAnswer();
?>