Search code examples
phpzend-frameworkzend-formcaptchazend-form-element

Abnormal issue with Zend_Form_Element_Captcha...?


In my Zend Application, I am facing an abnormal problem with the Captcha element. When I try to view the form in which I had used this Captcha element on my local machine it is working fine, but when I upload it to my Debian Server It is not working properly...!!!

Difference is as follows: enter image description here

As you can see on localhost the text inside the captcha is shown to the user, Where as on the Server[Debian], the text is missing!!!!!

I had used followig code to create the Captcha Element on my Zend Form:

    $elements = array();
    $captchaElement = new Zend_Form_Element_Captcha('captcha',
                                                array('label'   => "Ihr generierter Textcode:",
                                                      'captcha' => array('captcha' => 'Image',
                                                      'name'    => 'myCaptcha',
                                                      'wordLen' => 5,
                                                      'timeout' => 300,
                                                      'font'    => 'verdana.ttf',
                                                      'imgDir'  => 'captcha/',
                                                      'imgUrl'  => '/captcha/')
                                                     )
                                                 );
    $elements[] = $captchaElement;
    foreach ($elements as $index => $element)
    {
        $element->setAttrib('tabindex', ($index + 1));
    }

Can anyone tell me What mistake I am doing...?

Thanks In Advance.....


Solution

    1. Change this font to any other to check if Debian support it
    2. Set absolutely path for font and images:

      $captchaOptions = array(
          'label' => "Enter key",
          'captcha' => array(
              'captcha'   => 'Image',
              'wordLen'   => 4,
              'width'     => 197,
              'timeout'   => 120,
              'expiration'=> 300,
              'font'      => APPLICATION_PATH . '/../public/ttf/arial.ttf',
              'imgDir'    => APPLICATION_PATH . '/../public/images/captcha',
              'imgUrl'    => '/images/captcha/',
              'gcFreq'    => 5
          ),
      );
      
    3. What ZF version do you use? Cause after 1.7 there a bug in decorator, you have to set own decorator otherwise Zend_Captcha doesn't work:

      $captcha = new Zend_Form_Element_Captcha('Captcha', $captchaOptions);
      $captchaDecor = array_merge(array(new Decorator_Captcha()), $captcha->getDecorators());
      $captcha->setDecorators($captchaDecor);
      

    Decorator_Captcha file below

    class Decorator_Captcha extends Zend_Form_Decorator_Abstract
    {
        /**
         * Render captcha
         *
         * @param  string $content
         * @return string
         */
        public function render($content)
        {
            $element = $this->getElement();
            if (!method_exists($element, 'getCaptcha')) {
                return $content;
            }
    
            $view    = $element->getView();
            if (null === $view) {
                return $content;
            }
    
            $placement = $this->getPlacement();
            $separator = $this->getSeparator();
    
            $captcha = $element->getCaptcha();
            $markup  = $captcha->render($view, $element);
            switch ($placement) {
                case 'PREPEND':
                    $content = $content . $separator . $markup;
                    break;
                case 'APPEND':
                default:
                    $content = $markup . $separator .  $content;
            }
    
            return $content;
        }
    }