增加CaptchaController
实现验证码功能
1
修改App/config/resource.ini
captcha
=
default:captcha
2
修改library/common/pluin/myacl.php
$this->allow('guest'
,'default:captcha'
,array('index'
));
以上两步是增加访问权限,否则控制器无法识别。
3
修改前台模板link.php
<img id="imgcode"
style="margin-bottom:-5px;cursor:pointer;"
onclick="javascript:ChangImg()" src="<?=
$this->makeurl('captcha'
,'index'
);?>
">
<script type="text/javascript"
language="Javascript" >
function
ChangImg()
{
var
checkcode = $("#imgcode").get(0);
var
dt = new Date();
checkcode.src
= "<?=
$this->makeurl('captcha'
,'index'
);?>
";
}
</script>
4
新建
/app/modules/default/controllers/CaptchaController.php
<?php
require('CommonController.php');
class CaptchaController extends CommonController
{
function init() {
//
执行父类中的构造函数
parent::init();
}
/**
*
链接首页
*
*/
public function
indexAction()
{
Zend_Loader::loadClass('Common_Plugin_MyCaptcha');
$myCaptcha = new
Common_Plugin_MyCaptcha();
$myCaptcha->image();//生成图像
}
}
5
提交后的验证判断
<input
name="imagecode" id="imagecode" class="input"
value="" size="20" tabindex="20"
type="text"></label>
if ($this->_request->isPost())
{
//登陆验证
$filter = new Zend_Filter_StripTags();
require_once 'Zend/Session/Namespace.php'
;
$imagecode =
$filter->filter($this->_request->getPost('imagecode'
));
$authCode = new
Zend_Session_Namespace('Auth_Code'
);//
读取命名的空间
if
($authCode->imagecode!=$imagecode)
{
$this->view->error = "
验证码不正确
!";
echo $this->view->render('login.php'
);
exit;
}
}
最后附上MyCaptcha.php
<?php
/**
* 生成图像验证码
*
* 代码来自 ThinkPHP 的 Lib/ORG/Util/Image.class.php 文件,特此对 ThinkPHP 团队表示感谢
*
* Custom
*
* LICENSE:
*
* @category Common
* @package Plugin
* @copyright Copyright (c) PConline
* @license
* @version
*/
/**
* Zend_Controller_Plugin_Abstract
*/
require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Session/Namespace.php';
class Common_Plugin_MyCaptcha extends Zend_Controller_Plugin_Abstract
{
/**
* 生成图像验证码
*
* @param string $length 验证码长度
* @param string $mode 类型
* @param string $type 图像格式
* @param string $width 图像宽度
* @param string $height 图像高度
* @return string
*/
function image($length=4,$mode=1,$type='png',$width=40,$height=20)
{
$randval = $this->rand_string($length,$mode);
$authCode = new Zend_Session_Namespace('Auth_Code');//命名空间
$authCode->imagecode = $randval;
$width = ($length*9+10)>$width?$length*9+10:$width;
if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
$im = @imagecreatetruecolor($width,$height);
}else {
$im = @imagecreate($width,$height);
}
$r = Array(225,255,255,223);
$g = Array(225,236,237,255);
$b = Array(225,236,166,125);
$key = mt_rand(0,3);
// 背景色(随机)
$backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
// 边框色
$borderColor = imagecolorallocate($im, 100, 100, 100);
// 点颜色
$pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
@imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
@imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
$stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
// 干扰
for($i=0;$i<10;$i++){
$fontcolor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
}
for($i=0;$i<25;$i++){
$fontcolor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
}
@imagestring($im, 5, 5, 3, $randval, $stringColor);
$this->output($im,$type);
}
/**
* 输出图像
*
* @param string $im
* @param string $type
*/
function output($im,$type='png')
{
header("Content-type: image/".$type);
$ImageFun='Image'.$type;
$ImageFun($im);
imagedestroy($im);
}
/**
* 产生随机字串,字母和数字混合
*
* @param string $len 长度
* @param string $type 字串类型 (0 字母 1 数字 其它 混合)
* @param string $addChars 额外字符
* @return string
*/
function rand_string($len=4,$type='',$addChars='') {
$str ='';
switch($type) {
case 0:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
break;
case 1:
$chars= str_repeat('0123456789',3);
break;
case 2:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
break;
case 3:
$chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
break;
default :
// 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数
$chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
break;
}
if($len>10 ) {//位数过长重复字符串一定次数
$chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
}
if($type!=4) {
$chars = str_shuffle($chars);
$str = substr($chars,0,$len);
}else{
// 中文随机字
for($i=0;$i<$len;$i++){
$str.= substr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
}
}
return $str;
}
}
分享到:
相关推荐
主要介绍了Zend Framework生成验证码并实现验证码验证功能,较为详细的分析讲述了Zend Framework实现操作验证码操作的具体步骤与相关技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下
本文实例讲述了Zend Framework实现具有基本功能的留言本。分享给大家供大家参考,具体如下: 一个留言本…具有的基本功能就是.1.发表留言. 2.回复留言.3.管理留言(修改,删除等操作). 我这里只是写了基本的操作,比如...
$ config = Zend_Registry :: get ( 'config' )-> vk ;$ authUri = 'http://mysite.com/vk' ;$ api = new \ Vkontakte \ Api ( $ config -> id , $ config -> key , $ authUri , array ( 'offline' , 'notes' , '...
77:Zend framework数据库操作之编辑和视图函数 78:PHP+Flash多文件异步上传 swfupload 79:PHP上传RAR压缩包并解压目录 80:ADODB PHP 数据库万能引擎类(上) 81:ADODB PHP 数据库万能引擎类(下) 82:PHP开发...
BF-Blog是一款基于PHP5的、ZendFrameWork以及sqlite为数据库支持的blog(网络日志)程序。 演示地址:http://our.uni.cc/blog/nana BF-Blog的部分主要功能包括: *基于CSS+div布局的模板结构,模板样式修改容易; *...
系统架构:zendframework(精简) smarty(v3) WCMS功能 新闻分为文章、图集、专题 评论是独立的,可以不和新闻、用户结合起来 用户管理 系统组和用户组 可自定义模板 超时侦测 支持新闻扩展字段EAV设计...
目录 ... 小知识点 1 ...ZendFramework 73 快速体验 73 修改数据 74 增加数据 74 查询数据 74 memcached 76 telnet操作 76 php中使用memcached 78 把session数据放入memcache中 79 小知识点 80
PHP100视频教程77:Zend framework数据库操作之编辑和视图函数 PHP100视频教程78:PHP+Flash多文件异步上传 swfupload PHP100视频教程79:PHP上传RAR压缩包并解压目录 PHP100视频教程80:ADODB PHP 数据库万能...
同时由于强大的特性,也使其学习成本很高,所以目前大家看到的ZendFramework 2 产品相对于其他框架的产品来说并不多。作者(DBShop系统作者)知道国内使用ZF2(ZendFramework 2简称)开发项目的公司有一些,开发产品...
系统架构:zendframework(精简)+smarty(v3) WCMS功能 新闻分为文章、图集、专题 评论是独立的,可以不和新闻、用户结合起来 用户管理 系统组和用户组 可自定义模板 超时侦测 支持新闻扩展字段EAV设计 smarty...
使用Zend FrameWork 1.10.7内核,集成了SyntaxHighlighter代码高亮和facebox图片浏览插件。 这个BLOG很初级,很菜。开源的目的只是想和有兴趣的人一起学习提高。 安装使用说明: devcom_54dev.zip是数据库文件...
2、验证码启用和允许游客评论的设置;在后台 系统设置-站点信息中新增了这两个选项的设置。开启了验证码则用户登陆和注册,发表评论和留言时需要填写验证码;允许游客评论开启后,游客可以进行匿名评论,如果设置的...
13.6.2 第二范式(2NF) 209 13.6.3 第三范式(3NF) 210 13.7 数据库设计的方法与步骤 210 13.7.1 需求分析 211 13.7.2 概念设计的E-R方法 214 13.7.3 数据库的逻辑设计方法 215 13.7.4 物理设计阶段 218 13.7.5 ...