Zend_Form_Element Captcha的异常问题......?

时间:2012-02-04 10:43:04

标签: php zend-framework zend-form captcha zend-form-element

在我的Zend应用程序中,我遇到了Captcha元素的异常问题。当我尝试查看我在本地计算机上使用此Captcha元素的表单时,它工作正常,但是当我将其上传到我的Debian服务器时它无法正常工作...... !!!

差异如下: enter image description here

正如您在localhost上看到的那样,验证码内的文本会显示给用户,在服务器[Debian]上,文本丢失!!!!!

我使用了followig代码在我的Zend表单上创建了Captcha元素:

    $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));
    }

谁能告诉我我在做什么错误??

先谢谢.....

2 个答案:

答案 0 :(得分:4)

  1. 将此字体更改为任何其他字体以检查Debian是否支持
  2. 设置字体和图像的绝对路径:

    $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. 你使用什么ZF版本?因为1.7装饰后有一个错误,你必须设置自己的装饰器,否则Zend_Captcha不起作用:

    $captcha = new Zend_Form_Element_Captcha('Captcha', $captchaOptions);
    $captchaDecor = array_merge(array(new Decorator_Captcha()), $captcha->getDecorators());
    $captcha->setDecorators($captchaDecor);
    
  4. 下面的Decorator_Captcha文件

    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;
        }
    }
    

答案 1 :(得分:0)

正如RockyFord已在评论中写道,它不是ZF错误,而是PHP错误。

您将在已提供的答案中找到解决方案:Zend captcha Image generates blank