topical media & game development

talk show tell print

professional-search-08-seophp-include-simple-captcha.inc.php / php



  <?php
  // load Words library
  require_once('Numbers/Words.php');
  
  // SimpleCAPTCHA library
  class SimpleCAPTCHA
  {
    // verify answer
    function check_answer(answer, hash) 
    {
      return (md5(trim(answer) . _SERVER['SERVER_ADDR']) == hash);
    }
    
    // generate question
    function get_question(max_1, max_2)
    {
      // define standard question formats
      question_formats = array(
      'What is \%s plus \%s?',
      'What is the sum of \%s and \%s?', 
      'What is \%s added to \%s?',
      'What is \%s + \%s?'
      );
  
      // generate random numbers    
      number_1 = rand(0, max_1);
      number_2 = rand(0, max_2);
  
      // transforms the numbers to words    
      number_1_words = Numbers_Words::toWords(number_1);
      number_2_words = Numbers_Words::toWords(number_2);
  
      // generate a random question    
      question = sprintf(question_formats[rand(0, 
                               sizeof(question_formats) - 1)], 
                          number_1_words, 
                          number_2_words); 
      
      // returns the question and the hash of the result
      return array('question' => question, 
         'hash' => md5((number_1 + number_2) . _SERVER['SERVER_ADDR']));
    }
    
    // generates demo form
    function display_demo_form()
    {
    gq = SimpleCAPTCHA::get_question(1000, 10);
    echo '<form>';
    echo gq['question'];
    echo '<input type="text" name="response">';
    echo '<input type="hidden" name="hash" value="' . gq['hash'] . '">';
    echo '</form>';
    } 
  }
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.