帮忙看下PHP验证码问题出在哪里?

我的注册页面zhuce.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<form name=zhuce action=reg.php method=post>
<table>
<tbody>
<tr>
<td>用户名:</td>
<td><input type=text name=username></td>
<td>用户名长度为4~18个字符</td>
</tr></tbody></table>
<table>
<tbody>
<tr>
<td>登陆密码:</td>
<td><input type=text name=password></td>
<td rowspan=2>密码长度6~16位,字母区分大小写</td></tr>
<tr>
<td>重复登陆密码:</td>
<td><input type=text name=cpassword></td>
</tr></tbody></table>
<table>
<tbody>
<tr>
<td>性别:</td>
<td><input type=radio name=sex value=0 checked>男   <input type=radio name=sex value=1>女</td></tr>
<tr>
<td>真实姓名:</td>
<td><input type=text name=firstname></td>
</tr></tbody></table>
<table>
<tbody>
<tr>
<td>保密邮箱:</td>
<td><input type=text name=mail></td>
<td>填写、验证保密邮箱,通行证安全有保障</td>
</tr></tbody></table>
<table>
<tbody>
<tr>
<td>验证码:</td>
<td><input type=text name=usercheckcode><img alt=使用PHP实现登陆验证码 src=checknumber.php?act=init>
</td>
<td></td>
</tr></tbody></table>
<input type=submit value=注册帐号 title=确认注册>
</form>
</body>
</html>

这是我的验证码checknumber.php
<?php
session_start();

if($act == "init")
{
Header("Content-type: image/png");
srand(microtime() * 100000);
$login_check_number = strval(rand("1111","9999"));

session_register("login_check_number");
$h_img = imagecreate(40,17);
$c_black = ImageColorAllocate($h_img, 0,0,0);
$c_white = ImageColorAllocate($h_img, 255,255,255);
imageline($h_img, 1, 1, 350, 25, $c_black);
imagearc($h_img, 200, 15, 20, 20, 35, 190, $c_white);
imagestring($h_img, 5, 2, 1, $login_check_number, $c_white);
ImagePng($h_img);
ImageDestroy($h_img);

die();
}
?>

提交页面reg.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<?php
$name=$_POST['username'];
$passw=$_POST['password'];
$sex=$_POST['sex'];
$fname=$_POST['firstname'];
$mail=$_POST['mail'];
$checkcode=$_POST['usercheckcode'];

$sql="insert into members (username,password,sex,firstname,mail)
values ('$name','$passw','$sex','$fname','$mail')";

include_once("./checknumber.php");
if($number != $login_check_number || empty($number))
{
print("校验码不正确!");
die();
}

require('connect.php');
if($result)
{
echo '恭喜操作成功!<p>';
}
?>
</body>
</html>
可是老是出错,"校验码不正确",不知道错在哪,请高手帮帮忙,谢谢了

include_once("./checknumber.php");
if($number != $login_check_number || empty($number))
{
print("校验码不正确!");
die();
}
好像不对呀,你怎么把checknumber.php给引用过来了呀。checknumber.php生成的是一个随即的注册码呀:
$login_check_number = strval(rand("1111","9999"));
每次生成的都不一样的,你这样就执行了两次checknumber.php,每次生成的注册码都不一样的,当然提示注册码错误了。

正确的方法是:
在checknumber.php中保存生成的注册码:
$_SESSION['checknumber']=$login_check_number;
当然使用session前首先session_start();一下。

然后在reg.php 中就可直接用了(别忘了session_start();哦):
if($_SESSION['checknumber']!= $_POST['usercheckcode'])
{
print("校验码不正确!");
exit();
} else{……

还有,这句话要写在入库之前,写在后面没有用的,数据已经放到数据库里了再验证校验码还有什么意义?

另外给你推荐个网站:http://www.phpchina.com
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-12-14
楼上都说了,哈哈
这个srand(microtime() * 100000); 现在已经不用初始话了,rand()已经有这个功能了