PHP-based Simple Math Captcha
- Include the file. (Note:
session_start()
must be called before instantiating this class)
require_once 'MathCaptcha.php';
- Instantiate MathCaptcha
$cpa = new MathCaptcha();
- First call to the page (before user submit)
if( /* is this a submit? */ ){
$captcha_val = $_REQUEST['captcha'];
if( $cpa->validate($captcha_val) ){
echo 'Correct';
}else{
echo 'Incorrect';
}
}else{
// Initialize captcha
$cpa->reset_captcha();
}
- Display Captcha/Challenge text within a form (using
get_captcha_text()
method)
Simple
echo 'Solve this simple Math: ' . $cpa->get_captcha_text() . " = ?";
Rich Text
$captcha_text = 'Result of <i>{operand1}</i> {operator} <i>{operand2}</i> is?';
echo $cpa->get_captcha_text($captcha_text);
Example