A1:笑得海潮 B3:冒泡的崔 D2:Cornell University,Computer Vision Group H2:冰河的博客 G3:丕子博客 K1:MLA CHINA K4:斯坦福视觉实验室 L4:MIT 机器学习实验室
现在的位置: 首页技术>正文
cat_ico23 category
PHP+JQuery局部刷新验证码图片,Zend Framework框架
发表于599 天前 技术 评论数 4 ⁄ 被围观 1,594 次+

对于验证码,看不清点击刷新其实很正常,最近用这个,在Zend Framework下,用JQuery等,但是写的图像标签的click事件不能刷新图片,知道了是缓存、时间戳等问题,解决之。

1、首先是自己写的验证码插件,放在ZF的标准目录下面,library/Common/Plugin/ImgCode.php, 源码网上好多,可以参考一下即可。举个小例子:


/**
 +----------------------------------------------------------
 * 生成图像验证码
 +----------------------------------------------------------
 * @static
 * @access public
 +----------------------------------------------------------
 * @param string $length 位数
 * @param string $mode 类型 0 字母 1:数字,2:大写字母 3:小写字母 4:以上混合形式
 * @param string $type 图像格式
 * @param string $width 宽度
 * @param string $height 高度
 +----------------------------------------------------------
 * @return string
 +----------------------------------------------------------
 * @throws ThinkExecption
 +----------------------------------------------------------
 */
 function image2($length = 4, $mode = 4, $type = 'png', $width = 50, $height = 25) {
 $randval = $this->rand_string ( $length, $mode ); //生成随机数
 $authCode = new Zend_Session_Namespace ( 'Auth_Code' );
 $authCode->imagecode = $randval; //生成session

 //$_SESSION['IMGCODE']= $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, 255 );
 $g = Array (225, 236, 237, 255, 255 );
 $b = Array (225, 236, 166, 125, 255 );
 $key = mt_rand ( 0, 4 );

 //$backColor = imagecolorallocate ( $im, $r [$key], $g [$key], $b [$key] ); //背景色(随机)
 $backColor = imagecolorallocate ( $im, $r [4], $g [4], $b [4] ); //背景色(白色)
 $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 );
 }

2、然后在你的控制器中调用,假设是User的控制器吧,即UserController.php中写Action的函数:

function imgcodeAction() {
 Zend_Loader::loadClass ( 'Common_Plugin_ImgCode' );
 $imagecode = new Common_Plugin_ImgCode ();
 $imagecode->image2 ();
 }

3、在你的视图层就可以调用了,在要显示验证码的地方:

<input id="vdcode" type="text" value="" />
<img id="ImgCode" src="<?=$this->domain;?>/user/imgcode" alt="点击刷新" title="点击刷新" />

4、然后写刷新的js,用的JQuery。注意chgUrl这个函数,为了使每次生成图片不一致,即不让浏览器读缓存,所以需要加上时间戳
,不然点击刷新还是显示原图片,因为是从缓存里面读的,这个得注意哈:

$(document).ready(function() {
$("#ImgCode").click(function() {
 var img = $("#ImgCode");
 var domain=$("#domain").val();//你的域名

 url = domain+"/user/imgcode";
 img.attr("src", chgUrl(url));

 function chgUrl(url) {
 var timestamp = (new Date()).valueOf();
 urlurl = url.substring(0, 17);
 if ((url.indexOf("?") >= 0)) {
 urlurl = url + "&t=" + timestamp;
 } else {
 urlurl = url + "?t=" + timestamp;
 }
 return urlurl;
 }

 });

});

行了,是不是可以用了呢。

PHP+JQuery局部刷新验证码图片,Zend Framework框架:目前有4 条留言

  1. 先看看 : 2010年06月19日5:25 下午 回复

    很复杂的样子呢

  2. 北京格力空调维修 : 2010年06月19日8:50 下午 回复

    看不懂

  3. how much should i weigh : 2010年06月23日6:07 下午 回复

    hey whats your myspace page.

  4. 情侣空间 : 2011年02月04日10:58 下午 回复

    学习了,去试试去呢~!

给我留言


/ 快捷键:Ctrl+Enter

无觅相关文章插件,快速提升流量

不想听你唠叨×