javascript 图片验证的问题
用js和php做的。主要想用js做验证,不用到后台再验证。现在问题是这样,输入正确的验证码后,点提交,到wow.php页面后,再点返回,这时候要输入回刚才输入的验证码才能再次成功跳转,但是图片验证码已经更新了,不是原来的验证码数字。我应该怎么解决呢?
php生成图片代码:yzm.php
<?php
srand((double)microtime()*1000000);
$im=imagecreate(50,20); // 新建一个基于调色板的图像
$gray=imagecolorallocate($im,200,200,200);//imagecolorallocate -- 为一幅图像分配颜色
imagefill($im,0,0,$gray); //imagefill -- 区域填充
for($i=0;$i<4;$i++){
$str=mt_rand(1,3);
$size=mt_rand(3,6);
$authnum=substr($_GET['num'],$i,1);
imagestring($im,$size,(2+$i*10),$str,$authnum,imagecolorallocate($im,rand(0,130),rand(0,130),rand(0,130)));
} //imagestring -- 水平地画一行字符串
imagepng($im);
imagedestroy($im);
?>
html页面输入: login.html
<html>
<head>
<title>js---判断验证码码是否正确</title>
<script >
function yzm(form){
var num1=Math.round(Math.random()*10000000);
var num=num1.toString().substr(0,4);
form.check2.value=num;
document.write("<img name=codeimg width=50 heigh=20 src='yzm.php?num="+num+"'>");
}
function code(form){
var num1=Math.round(Math.random()*10000000);
var num=num1.toString().substr(0,4);
form.check2.value=num;
document.codeimg.src="yzm.php?num="+num;//这里面的num就是验证码的值
}
function checkYzm(form){
if(form.check.value == ""){
document.getElementById('result').innerHTML = "验证码不能为空";
}else if(form.check.value != form.check2.value){
document.getElementById('result').innerHTML = "验证码错误,请重新输入";
}else{
document.getElementById('result').innerHTML = "验证码输入正确";
}
}
function postForm(form) {
if(form.check.value == "" || form.check.value != form.check2.value)
{
document.getElementById('check').value = '';
document.getElementById('check').focus();
}
else
{
form.submit();
}
}
</script>
<style type="text/css">
#result {height:240px;width:150px;background-color:red;}
</style>
</head>
<body>
<form id="login" name="login" method="post" action="wow.php">
<table>
<tr>
<td width="46" height="18">
<input id="check" name="check" type="text" width="46" onBlur="return checkYzm(login)" >
<input id="check2" name="check2" type="hidden" />
</td>
<td width="52" height="18" onClick="javascript:code(login)">
<script>yzm(login);</script>
</td>
</tr>
<tr>
<td colspan=2>
<input type="button" id="btnSubmit" name="btnSubmit" value="提交" onclick="postForm(login)"/>
</td>
</tr>
</table>
<div id="result"></div>
</form>
</body>
</html>
wow.php 验证码匹配正确后跳转过来的页面
<?php
echo "wow!you done it!";
?>