<1>.图像因其本身有错无法访问。
这里我并没有使用perl的captcha来生成图像,而是面向对象了一个之前用过的代码。
1.GD必须采用IE来调试。这样才能显示出详细错误来。
2.禁止调用视图
// disable auto-rendering since we're outputting an image
$this->_helper->viewRenderer->setNoRender();
3.ZF的session()的用法
$session = new Zend_Session_Namespace('captcha');
// check for existing phrase in session
$phrase = null;
if (isset($session->phrase) && strlen($session->phrase) > 0)
{
$phrase = $session->phrase;
}
// write the phrase to session
else
{
$phrase = $this->getRandomCode();
$session->phrase =$phrase;
}
3.获取表单值并验证
$session = new Zend_Session_Namespace('captcha');
//要使用$request->getPost('captcha'),必须继承Zend_Controller_Action类
$this->captcha = $this->sanitize($request->getPost('captcha'));
if ($this->captcha != $session->phrase)
$this->addError('captcha', 'Please enter the correct phrase');
// if no errors have occurred, save the user
if (!$this->hasError()) {
$this->user->save();
unset($session->phrase);
}
4.新的smarty用法
$smarty = new Smarty();//----------------------------------------------->getEngine(),在构造函数中创建一次
$smarty->template_dir = $config->path->templates;
$smarty->compile_dir = $config->paht->."/tmp/templates_c";
$name = "lbzhao";
$smarty->assign('news',$name);//---------------------------------------->$this->view->news = $name;
$smarty-display('news/index.tpl');//-------------------------------------->render();
//视图的调用
$this->_redirect(''/account/registerover'');
$this->_forward('register');
5.写SQL的用法
<?php $ranking = 1; foreach ($_POST[$key] as $id) { if (!array_key_exists($id, $items)) continue; $query = sprintf('update items set ranking = %d where item_id = %d', $ranking, $id); mysql_query($query); $ranking++; } ?>
6.生成验证的代码
<?php class UtilityController extends CustomControllerAction { //r随机字符串种子,可以换成字母或其他英文字符 private $glbVerifySeed = "123456789abcdefg"; //获取随机数字字符串 private function getRandomCode($length=6) { $bgnIdx = 0; $endIdx = strlen($this->glbVerifySeed)-1; $code = ""; for($i=0; $i<$length; $i++) { $curPos = rand($bgnIdx, $endIdx); $code .= substr($this->glbVerifySeed, $curPos, 1); } return $code; } //输出校验码图像 private function doOutputImg($string, $imgWidth, $imgHeight, $imgFont, $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) { $image = imagecreatetruecolor($imgWidth, $imgHeight); //用白色背景加黑色边框画个方框 $backColor = imagecolorallocate($image, 255, 255, 255); $borderColor = imagecolorallocate($image, 0, 0, 0); imagefilledrectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor); imagerectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor); $imgFgColor = imagecolorallocate ($image, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]); $this->doDrawStr($image, $string, $imgFgColor, $imgFont); $this->doPollute($image, 64); //return $image; header('Content-type: image/png'); imagepng($image); imagedestroy($image); } //画出校验码 private function doDrawStr($image, $string, $color, $imgFont) { $imgWidth = imagesx($image); $imgHeight = imagesy($image); $count = strlen($string); $xpace = ($imgWidth/$count); $x = ($xpace-6)/2; $y = ($imgHeight/2-8); for ($p = 0; $p<$count; $p ++) { $xoff = rand(-2, +2); $yoff = rand(-2, +2); $curChar = substr($string, $p, 1); imagestring($image, $imgFont, $x+$xoff, $y+$yoff, $curChar, $color); $x += $xpace; } return 0; } //画出一些杂点 private function doPollute($image, $times) { $imgWidth = imagesx($image); $imgHeight = imagesy($image); for($j=0; $j<$times; $j++) { $x = rand(0, $imgWidth); $y = rand(0, $imgHeight); $color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255)); imagesetpixel($image, $x, $y, $color); } } public function captchaAction() { //session_start(); $session = new Zend_Session_Namespace('captcha'); // check for existing phrase in session $phrase = null; if (isset($session->phrase) && strlen($session->phrase) > 0) $phrase = $session->phrase; // write the phrase to session else { $phrase = $this->getRandomCode(); $session->phrase =$phrase; } $imgWidth = 100; $imgHeight = 20; $imgFont = 6; $this->doOutputImg($phrase, $imgWidth, $imgHeight, $imgFont); // disable auto-rendering since we're outputting an image $this->_helper->viewRenderer->setNoRender(); } } /** *这个是使用perl生成的。 * */ /*public function captchaAction() { $session = new Zend_Session_Namespace('captcha'); // check for existing phrase in session $phrase = null; if (isset($session->phrase) && strlen($session->phrase) > 0) $phrase = $session->phrase; $captcha = Text_CAPTCHA::factory('Image'); $opts = array('font_size' => 20, 'font_path' => Zend_Registry::get('config')->paths->data, 'font_file' => 'yahei.ttf'); $captcha->init(120, 60, $phrase, $opts); // write the phrase to session $session->phrase = $captcha->getPhrase(); // disable auto-rendering since we're outputting an image $this->_helper->viewRenderer->setNoRender(); header('Content-type: image/png'); echo $captcha->getCAPTCHAAsPng(); }*/ ?>