public function indexAction() { $this->codeSession = new Zend_Session_Namespace('code'); //在默认构造函数里实例化 $captcha = new Zend_Captcha_Image(array('font' => APPLICATION_PATH . '/../public/fonts/ShadowsAroundUs.ttf', 'fontsize' => 24, 'imgdir' => './images/', 'session' => $this->codeSession, 'width' => 100, 'height' => 50, 'wordlen' => 5, 'LineNoiseLevel' => 0, 'DotNoiseLevel' => 20)); //字母数 $captcha->setExpiration(1); $captcha->setGcFreq(3); //设置删除生成的旧的验证码图片的随机几率 $captcha->generate(); //生成图片 $this->view->ImgDir = $captcha->getImgDir(); $this->view->captchaId = $captcha->getId(); //获取文件名,md5编码 $this->codeSession->code = $captcha->getWord(); //获取当前生成的验证字符串 $this->view->code = $this->codeSession->code; }
public function viewAction() { $id = $this->_request->getParam('id'); $captchaCode = $this->_request->getParam('captcha_code'); $modelBlog = new Page(); $blog = $modelBlog->getPage($id); if ($blog) { $this->view->blog = $blog; // tags $modelTags = new Tags(); $where = array('blog_id' => $id); $tags = $modelTags->getTags($where); if ($tags) { $this->view->tags = $tags; } // 评论 $modelComment = new Comment(); $comments = $modelComment->getComments($id); $this->view->comments = $comments; $dataComment = $this->_request->getPost(); // 获取表单提交值 if ($dataComment) { if ($dataComment['captcha'] == $captchaCode) { // 定义过滤规则 $filters = array('name' => array('StringTrim'), 'comment' => 'StripTags'); // 定义验证规则 $validators = array('name' => array(array('StringLength', 3, 16), 'NotEmpty', Zend_Filter_Input::MESSAGES => array(array(Zend_Validate_StringLength::INVALID => "请输入一个合法的字符串", Zend_Validate_StringLength::TOO_SHORT => "请输入字符长度为3-16", Zend_Validate_StringLength::TOO_LONG => "请输入字符长度为3-16"))), 'email' => array('EmailAddress', Zend_Filter_Input::MESSAGES => array(array(Zend_Validate_EmailAddress::INVALID_FORMAT => "邮件格式不正确,请重新输入。"))), 'comment' => array()); // 实例化过滤器并进行过滤验证 $data = $_POST; $filterPost = new Zend_Filter_Input($filters, $validators, $data); if ($filterPost->hasInvalid() || $filterPost->hasMissing()) { $messages = $filterPost->getMessages(); foreach ($messages as $message) { foreach ($message as $value) { echo $value . "<br />"; } } } // 将经过验证的数据写入数据库 $modelComment = new Comment(); $newComment = $modelComment->createComment($pid = $id, $filterPost->name, $filterPost->email, $filterPost->comment); if ($newComment) { $this->_redirect('/blog/view/id/' . $id); } else { echo "评论提交出错!"; } } else { echo "验证码错误,请刷新后重新输入。"; } } // 生成验证码 $this->captcha_session = new Zend_Session_Namespace('captcha'); //在默认构造函数里实例化 $captcha = new Zend_Captcha_Image(array('font' => 'images/SIMYOU.TTF', 'session' => $this->captcha_session, 'fontsize' => 15, 'imgdir' => 'images/code/', 'width' => 120, 'height' => 30, 'gcFreq' => 3, 'dotNoiseLevel' => 5, 'lineNoiseLevel' => 1, 'wordlen' => 4)); $captcha->generate(); // 生成图片 // 界面方式 $this->view->img_dir = $captcha->getImgDir(); $this->view->captcha_id = $captcha->getId(); //图片文件名,md5编码 $this->view->captcha_code = $captcha->getWord(); $this->view->id = $id; } else { echo "该博客文章不存在!"; } }