Введение в Zend Framework

 Learning Zend Framework


 Zend Framework Reference

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Ttranslation 27.4% Update 2010-11-28 - Revision 23238 - Version ZF 1.11.x

    63.25. Zend_Service_ReCaptcha

    63.25.1. Introduction

    Zend_Service_ReCaptcha provides a client for the reCAPTCHA Web Service. Per the reCAPTCHA site, "reCAPTCHA is a free CAPTCHA service that helps to digitize books." Each reCAPTCHA requires the user to input two words, the first of which is the actual CAPTCHA, and the second of which is a word from some scanned text that Optical Character Recognition (OCR) software has been unable to identify. The assumption is that if a user correctly provides the first word, the second is likely correctly entered as well, and can be used to improve OCR software for digitizing books.

    In order to use the reCAPTCHA service, you will need to sign up for an account and register one or more domains with the service in order to generate public and private keys.

    63.25.2. Simplest use

    Instantiate a Zend_Service_ReCaptcha object, passing it your public and private keys:

    Пример 63.123. Creating an instance of the reCAPTCHA service

    $recaptcha = new Zend_Service_ReCaptcha($pubKey$privKey);

    To render the reCAPTCHA, simply call the getHTML() method:

    Пример 63.124. Displaying the reCAPTCHA

    echo $recaptcha->getHTML();

    When the form is submitted, you should receive two fields, 'recaptcha_challenge_field' and 'recaptcha_response_field'. Pass these to the reCAPTCHA object's verify() method:

    Пример 63.125. Verifying the form fields

    $result $recaptcha->verify(

    Once you have the result, test against it to see if it is valid. The result is a Zend_Service_ReCaptcha_Response object, which provides an isValid() method.

    Пример 63.126. Validating the reCAPTCHA

    if (!$result->isValid()) {
    // Failed validation

    It is even simpler to use the reCAPTCHA Zend_Captcha adapter, or to use that adapter as a backend for the CAPTCHA form element. In each case, the details of rendering and validating the reCAPTCHA are automated for you.

    63.25.3. Hiding email addresses

    Zend_Service_ReCaptcha_MailHide can be used to hide email addresses. It will replace a part of an email address with a link that opens a popup window with a reCAPTCHA challenge. Solving the challenge will reveal the complete email address.

    In order to use this component you will need an account to generate public and private keys for the mailhide API.

    Пример 63.127. Using the mail hide component

    // The mail address we want to hide
    $mail 'mail@example.com';

    // Create an instance of the mailhide component, passing it your public
    // and private keys, as well as the mail address you want to hide
    $mailHide = new Zend_Service_ReCaptcha_Mailhide();

    // Display it

    The example above will display "m...@example.com" where "..." has a link that opens up a popup window with a reCAPTCHA challenge.

    The public key, private key, and the email address can also be specified in the constructor of the class. A fourth argument also exists that enables you to set some options for the component. The available options are listed in the following table:

    Таблица 63.19. Zend_Service_ReCaptcha_MailHide options

    Option Description Expected Values Default Value
    linkTitle The title attribute of the link string 'Reveal this e-mail address'
    linkHiddenText The text that includes the popup link string '...'
    popupWidth The width of the popup window int 500
    popupHeight The height of the popup window int 300

    The configuration options can be set by sending them as the fourth argument to the constructor or by calling setOptions($options), which takes an associative array or an instance of Zend_Config.

    Пример 63.128. Generating many hidden email addresses

    // Create an instance of the mailhide component, passing it your public
    // and private keys, as well as some configuration options
    $mailHide = new Zend_Service_ReCaptcha_Mailhide();
    'linkTitle' => 'Click me',
    'linkHiddenText' => '+++++',

    // The mail addresses we want to hide
    $mailAddresses = array(

    foreach (
    $mailAddresses as $mail) {

    digg delicious meneame google twitter technorati facebook