1.新建一个php
<html>
<head>
<title>申请帐号</title>
</head>
<form method="post" action="insert.php" name="send" onSubmit="return Check()">
<td width="210" class="p11" valign="bottom"><font color="#FF6699">*</font>为必填项</td>
<p>用户名:
<input type="text" name="username" size="20" class="c3a">
<font color="#FF6699">*</font>
</p>
<p>密码:
<input type="password" name="password" size="20" class="c3a">
<font color="#FF6699">*</font></p>
<p>确认密码:
<input name="cpassword" type="password" id="cpassword">
<font color="#FF6699">*</font></p>
<p>email:<label>
<input name="email" type="text" id="email">
</label>
<font color="#FF6699">*</font></p>
<label></label>
性别:男
<input type="radio" name="sex" value="male" />
<label></label>
<p>女
<input type="radio" name="sex" value="female" />
保密
<input type="radio" name="sex" value="secret" />
</p>
<p>
验证码:
<input name="code" type="text" class="c3a" size="8">
<img src="code.php?" alt="点击刷新" onClick="this.src=this.src+'?'" /></p>
<p>
<input type="submit" value="下一步" >
<input type="reset" value="重来">
</p>
</form>
<script language="javascript">
function Check()// 验证表单数据有效性的函数
{
if (document.send.username.value=="")
{
window.alert('请输入用户名!');
return false;
}
if (document.send.username.value.length<4)
{
window.alert('用户名长度必须大于4!');
return false;
}
if (document.send.password.value=="")
{
alert('请输入密码!');
return false;
}
if (document.send.password.value.length<6)
{
alert('密码长度必须大于6!');
return false;
}
if (document.send.password.value!= document.send.cpassword.value)
{
alert('确认密码与密码不一致!');
return false;
}
if (document.send.email.value=="")
{
alert('请输入Email!');
return false;
}
if(document.send.email.value.indexOf("@")==-1)
{
alert('请输入有效的email地址!'); return false;
}
if (document.send.code.value=="")
{
alert('请输入验证码!');
return false;
}
return true;
}
</script>
</body>
</html>
2.其中插入的insert.php
<pre name="code" class="php"><?php session_start();
$conn=@ysqli_connect("localhost","root","root","userman");
$conn->query('set names utf8');
if(empty($conn))
{
die("数据库连接失败");
}
$UserName=$_POST['username'];
$pwd=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();
$code=strtolower($_POST["code"]);
$str="select * from users where username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row)
{
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=zhuce.php>返回</a>";
}
else {
if( $_SESSION['sname']==$code)
{
echo $temp="验证码成功";
$sql="INSERT INTO users VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
$_SESSION['mail']="注册成功,请登陆";
echo "<script>window.location.href='login1.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
else {echo "验证码错误";}
}
?>
3.其中的code.php
<pre name="code" class="php"><?php
header("Content-Type:image/png");
session_start();
$code = "";
$arr = array();
for($i=0;$i<4;$i++){
$arr[$i] = rand(0,9);
$code .= (string)$arr[$i];
}
//设置入session中,方便比对
$_SESSION["name"] = $code;
//开始绘图
$width = 100;
$height = 25;
$img = imagecreatetruecolor($width,$height);
//填充背景色
$backcolor = imagecolorallocate($img,0,0,0);
imagefill($img,0,0,$backcolor);
//获取随机较深颜色
for($i=0;$i<4;$i++){
$textcolor = imagecolorallocate($img,rand(50,180),rand(50,180),rand(50,180));
imagechar($img,12,7+$i*25,3,(string)$arr[$i],$textcolor);
}
//显示图片
imagepng($img);
//销毁图片
imagedestroy($img);
?>
弄完以后,运行一下,一个简单的注册界面就有了!如果代码有什么不对的地方,请多多指教!!