【问题标题】:Abnormal issue with Zend_Form_Element_Captcha...?Zend_Form_Element_Captcha 的异常问题...?
【发布时间】:2012-07-13 01:19:04
【问题描述】:

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

区别如下:

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

我曾使用以下代码在我的 Zend 表单上创建验证码元素:

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

谁能告诉我我做错了什么...?

提前致谢.....

【问题讨论】:

  • 生产服务器上有字体吗?
  • @bububaba : 是的,我在服务器上确实有 verdana.ttf 字体,但我不确定 Debian 是否支持它。
  • stackoverflow.com/q/7001725/1145086,这在linux上的一些php版本中很常见。当前版本应该已修复,但您的主机可能尚未升级。
  • 检查字体文件是否有读取权限。

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


【解决方案1】:
  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 之后装饰器有 bug,你必须设置自己的装饰器,否则 Zend_Captcha 不起作用:

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

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

【讨论】:

    【解决方案2】:

    正如 RockyFord 在 cmets 中所写的,这不是 ZF 错误,而是 PHP 错误。

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

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 2011-05-13
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多