刷新验证码

来源:互联网 发布:快乐十分助赢软件 编辑:程序博客网 时间:2024/05/22 12:35

用户登录页面当点击验证码时,验证码会更新

思路步骤:1、创建好验证码

          2、创建用户登录表单,当点击验证码时更换新的

1yanzhengma.php

//当刷新页面时验证码会更新

<?php

ob_clean();

$im=imagecreatetruecolor(80,20);//创建画布

$bgcolor=imagecolorallocate($im,220,230,230);//调色

$border=imagecolorallocate($im,0,0,255);//设置边框的颜色

$tcolor=imagecolorallocate($im,255,0,0);

$green=imagecolorallocate($im,0,0xff,0);

imagefill($im,10,10,$bgcolor);//填充背景色

imagerectangle($im,1,1,79,19,$border);//绘制边框,也就是一个矩形,画布(0,0,80,20)边框(1,1,79,19)

$num = rand(48,122);

//rand产生一个随机整数rand(5,10)产生5-10的随机数包括5,10

for($i=0;$i<4;$i++){

$num_case=rand(0,2);//产生随机数0-2,根据数值的不同决定产生的是数字|小写|大写

switch($num_case){

case 0:$num=rand(48,57);break;//数字

case 1:$num=rand(65,90);break;//大写

default:$num=rand(97,122);//小写

}

$text[$i]=sprintf("%c",$num);//将随机产生的assii码转换为相应的字符

imagettftext($im,rand(10,20),rand(0,30),15*$i,20,$tcolor,"ahronbd.ttf",$text[$i]);

}//用 TrueType 字体向图像写入文本,ahronbd.ttf是一种字体格式

for($i=0;$i<100;$i++){

imagesetpixel($im,rand(1,79),rand(1,22),$green);//产生100个点

}

session_start();

$_SESSION["ckcode"]=implode($text);//保存到session中。和4logon.php$_SESSION["ckcode"]联系

header("Content-type:image/png");//设置输出类型

imagepng($im);//PNG格式将图像输出到浏览器或文件

imagedestroy($im);//销毁图像释放内存

?>

2denglu.php

<script language="javascript" type="text/javascript">

function refreshcode(obj,url){

obj.src=url+"?nowtime="+Math.random();//random来返回 到 之间的随机数

}

</script>

</head>

<body>

<center><h1>用户登录</h1></center>

<form action="" method="post">

用户名:<input type="text" name="username"><br>

密码:<input type="password" name="password"><br>

验证码:<input type="text" name="ckcode">

<img src="yanzhengma.php" style="cursor:pointer;" onclick="refreshcode(this,this.src);"><br><!--点击验证码会刷新,出新的验证码-->

<input type="submit" value="登录">

<input type="button" onclick="" value="取消">

</form>

<a href="2zhuce.php">注册新用户</a>

原创粉丝点击