<?php //启动session会话 session_start(); header('content-type:text/html;charset=utf-8'); //接收表单数据 $username=trim($_POST['username']); $password=md5(trim($_POST['password'])); //密码MD5加密 $password_confirm=md5(trim($_POST['password'])); $code=$_POST['code']; if($username==''||$password==''||$password_confirm==''){ echo "<script>alert('请确定信息的完整性!');history.back();</script>"; }elseif($code!=$_SESSION['var_code']){ echo "<script>alert('验证码不对哦!');history.back();</script>"; }else{ if($password==$password_confirm){ //2.连接MYSQL,选择数据库 $link = mysql_connect('localhost','root','root') or die("数据库连接失败!"); mysql_query('set names utf8'); mysql_query('use `demodb`'); //执行sql $sql="select * from `manager` where `username`='$username'"; $res=mysql_query($link,$sql); $nums=mysql_num_rows($res); if($nums){ //存在 echo "<script> alert('用户名已存在,请换个名称!');history.back();</script>"; exit; }else{ $sql_insert="insert into manager values('$username','$password')"; $res_insert=mysql_query($link,$sql_insert); if($res_insert){ echo "<script>alert('注册成功!');history.back();</script>"; }else{ echo "<script>alert('注册失败!');history.back();</script>"; } } } else{ echo "<script>alert('密码不一致!');history.back();</script>"; } } ?>