IndexCOntroller.php
<?php
class IndexController extends Zend_Controller_Action
{
private $codeSession; //定義一個全局 Zend_Session_Namespace
function init() //__construct 代替初始化函數
{
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();
}
/*
* Action(動作)!
*/
function indexAction()
{
$this->codeSession = new Zend_Session_Namespace('code'); //在默認構造函數裏實例化
$captcha = new Zend_Captcha_Image(array('font'=>'./public/images/faktos.ttf', //字體文件路徑
'fontsize'=>24, //字號
'imgdir'=>'./public/images/code/', //驗證碼圖片存放位置
'session'=>$this->codeSession, //驗證碼session值
'width'=>120, //圖片寬
'height'=>50, //圖片高
'wordlen'=>5 )); //字母數
$captcha->setGcFreq(3); //設置刪除生成的舊的驗證碼圖片的隨機機率
$captcha->generate(); //生成圖片
$this->view->ImgDir = $captcha->getImgDir();
$this->view->captchaId = $captcha->getId(); //獲取文件名,md5編碼
$this->codeSession->code=$captcha->getWord(); //獲取當前生成的驗證字符串
// echo $this->codeSession->code;
echo $this->view->render('index.html');
}
}
index.html
<span id="captcha">
<span id="captcha"><img src="<?php echo $this->baseUrl,$this->ImgDir,$this->captchaId ?>.png" border="0" />
</span>