对于验证码,看不清点击刷新其实很正常,最近用这个,在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开发个中型站点,请推荐些技术吧](http://www.spiralteck.com/images/right-img.jpg)
![[源码] Js和JQuery实现DIV等的显示和隐藏](http://pathology.jhu.edu/doran/images/jquery.jpg)



很复杂的样子呢
看不懂
hey whats your myspace page.
学习了,去试试去呢~!