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>