2012-04-08 59 views
0

我试图安装Captcha(https://github.com/kolanos/kohana-captcha)到Kohana 3.1。安装了模块,但仍然搞砸了,有些问题找不到答案。如果你特别的东西我不明白它是如何工作的,这里的代码: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 ,当你调用这个方法(默认配置)是真的。因此,返回和底层代码不应执行,但调试器说相反......它是如何工作的?

  2. 稍后在变量$ function中通过串联“image”和$ thos-> image_type(如上所示=“png”)传递一个字符串。它变成了一个函数的名称,它以png格式(“imagepng”)提供图像。下面这行代码使用了模糊的语法:$ function($ this-> image);这些线是做什么的?

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

回答

0
  1. 在配置的任何地方没有设置html值。如果$ html为真,即image_render(TRUE),则返回仅适用。如果您拨打image_render(1)image_render('some string'),它将不会执行,因为在if声明中使用了===运算符。检查here了解更多转换为布尔类型。
  2. 第一行评估函数名称(例如imagepng),第二行调用此函数。有关更多详细信息,请参阅variable functions
+0

2.非常感谢!我不知道这种可能性 1.也许我不是这样解释问题的本质。没有转换布尔类型。方法调用的TRUE以bool类型传输。 我明白它是如何工作的。 当您调用image_render(TRUE)时,会生成HTML代码以在表单上插入验证码。 IMG元素包含到验证码的链接。当您尝试浏览器上传时,图片是调用方法image_render(FALSE),服务器会为生成的验证码提供所有必需的HTTP标头。 – Vladimir 2012-04-12 16:02:32