antispam avoid spam captcha validation Completely Automated Public Turing test to tell Computers and Humans Apart

CAPTCHA validation

CAPTCHA validation is "a type of challenge-response test used in computing to determine whether or not the user is human" (for details see the related Wikipedia page) - in short, it's a great way to avoid form spamming.

To use the CAPTCHA validation, follow these steps:

  • you can freely use your favourite CAPTCHA generator (eg. PEAR Text_CAPTCHA, pwImage, etc.) - the only requirement is to store the string used in the image in the $_SESSION array. In general a CAPTCHA generator script is like:

    <?php

    $captcha 
    Text_CAPTCHA::factory'Image' );
    $captcha->init(15060NULL, array( 'font_file' => 'arial.ttf' ) ); 
    header("Content-type: image/jpeg"); 
    echo 
    $captcha->getCAPTCHAAsJPEG(); 
    $_SESSION['captchapass'] = $captcha->getPhrase();

    ?>
  • attach an <img src="..."> tag pointing to your CAPTCHA script: use the postfix setting of an inputText element 
  • add the captcha validation to the inputText element: define the key of the $_SESSION variable ('captchapass' in the above example) in the 'index' setting of the validation array of the element - and now you're done! 

<?php 

$config 
= Array( 

  
'element' => Array( 

    
'type' => '...'
    
// ...further element settings here... 

    
'validation' => Array(  

      Array(
        
'type'     => 'captcha',

        
'index'    => 'captchatext',
        
// 'index' is the $_SESSION array key:
        // if your CAPTCHA image generator
        // uses $_SESSION['captchatext'] to save
        // the CAPTCHA letters, use 'captchatext' 
        // here

        
'casesensitive' => 1// default: 0
        // CAPTCHAs are typically case insensitive

        
'help'    => 'Help message value for this validator only'
      
)
    )
  )
);

?>


Live examples:

form validation for developers!

clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain forms
learn more
Bookmark and Share