2012-04-08 64 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