【问题标题】:Kohana 3.1 Module Captcha. How it works?Kohana 3.1 模块验证码。这个怎么运作?
【发布时间】:2012-04-08 18:15:24
【问题描述】:

我尝试将 Captcha (https://github.com/kolanos/kohana-captcha) 安装到 Kohana 3.1。模块已安装,但仍然搞砸了,有问题无法找到答案。如果你特别是我不明白它是如何工作的,这里是代码:

/**
    * Returns the img html element or outputs the image to the browser.
    *
    * @param boolean $html Output as HTML
    * @return mixed HTML, string or void
    */
   public function image_render($html)
   {
      // Output html element
      if ($html === TRUE)
         return '<img src="'.url::site('captcha/'.Captcha::$config['group']).'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" alt="Captcha" class="captcha" />';

      // Send the correct HTTP header
        Request::instance()->headers['Content-Type'] = 'image/'.$this->image_type;
        Request::instance()->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0';
        Request::instance()->headers['Pragma'] = 'no-cache';
        Request::instance()->headers['Connection'] = 'close';

      // Pick the correct output function
      $function = 'image'.$this->image_type;
      $function($this->image);

      // Free up resources
      imagedestroy($this->image);
   }

...这不在本课程中http://prog-school.ru/forum/go.php?https://github.com/kolanos/kohana-captcha/blob/master/classes/captcha.php

问题:

  1. 在变量$html中,调用该方法时(默认为config)为true。因此是返回和底层代码不应该被执行,但调试器却说反了……它是如何工作的?

  2. 稍后在变量 $function 中通过连接 "image" 和 $thos->image_type 传递一个字符串(如上所示 = "png")。结果是带有函数名称的行,它给出了 png 格式的图像(“imagepng”)。并且以下行使用了晦涩的语法: $function($this->image);这些行是做什么的?

我希望有人能帮助我了解它是如何工作的。

【问题讨论】:

    标签: php kohana


    【解决方案1】:
    1. 配置中的任何位置都没有设置html 值。仅当 $html 为 TRUE 即 image_render(TRUE) 时,返回才有效。如果您调用image_render(1)image_render('some string'),它将不会执行,因为=== 运算符在if 语句中使用。检查here 以了解更多转换为布尔类型的信息。
    2. 第一行计算函数名(例如imagepng),第二行调用这个函数。请参阅variable functions 了解更多详情。

    【讨论】:

    • 2.非常感谢!我不知道这种可能性 1. 也许我没有这么解释问题的本质。没有转换布尔类型。 TRUE 到方法调用被传输为 bool 类型。我明白它是如何工作的。当您调用 image_render (TRUE) 时,会生成 HTML 代码以在表单上插入验证码。 IMG 元素包含到验证码的链接。当您尝试浏览器上传图片时,调用方法 image_render (FALSE),服务器将生成的验证码与所有必要的 HTTP 标头一起提供。
    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多