php+mysql实现用户与管理员注册和登录功能

效果图

一、准备工作

1、在mysql中创建数据库member

CREATE DATABASE MEMBER;

2、根据id、username、psd、email、sex、createtime、ad七个字段建立数据表userinfo

CREATE TABLE userinfo(
    id INT(8) PRIMARY KEY,
    username VARCHAR(32) NOT NULL,
    psd VARCHAR(32) NOT NULL,
    email VARCHAR(32) NOT NULL,
    sex TINYINT(1) NOT NULL,
    createtime DATE NOT NULL,
    ad TINYINT(1) NOT NULL
)

3、插入几条用户信息

insert into userinfo values(1,'张三','1223456','1233@163.com','1','2002-11-01','0');
insert into userinfo values(2,'李四','1234256','1223@163.com','0','2004-02-01','0');
insert into userinfo values(3,'王五','12123456','1243@163.com','1','2001-03-11','0');
insert into userinfo values(4,'赵六','1252246','1237@163.com','1','2005-04-21','0');
insert into userinfo values(5,'钱七','1236356','1283@163.com','1','2001-05-11','0');

二、用户注册

1、注册前端 register.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
    <style>
        .all{
            width: 100%;
            height: 100%;
            align-items: center;
            background: rgba(169,234,240,0.5);
            background-image: url("图片素材/BG.gif");
            background-size: 100% 100%;
            background-repeat: no-repeat;
        }

        .main{
            width: 600px;
            height: 700px;           
            margin:auto;
            background: url("图片素材/dapu.png");
            color: white;
            padding-top: 1px;
            margin-top: 60px;
            margin-bottom: 70px;
      box-shadow: rgba(0, 0, 0, 0.09) 0px 2px 1px, rgba(0, 0, 0, 0.09) 0px 4px 2px, rgba(0, 0, 0, 0.09) 0px 8px 4px, rgba(0, 0, 0, 0.09) 0px 16px 8px, rgba(0, 0, 0, 0.09) 0px 32px 16px;
        }
        .form{
            width: 300px;
            height: 450px;
            background-color:rgba(149,154,246,0.65) ;
            margin-left:160px;
            margin-top: 120px;
            box-shadow: rgba(3, 102, 214, 0.3) 0px 0px 0px 3px;
        }
        .form td{
            font-size: 20px;
            
        }
        h1{
            font-size: 24px;
        }
        h1 a{
            color: navy;text-decoration: none;margin-right: 15px;
        }
        h1 a:last-child{
            margin-right: 0;
        }
        h1 a:hover{
            color: brown;text-decoration: underline
        }
        .current{
            color:brown;
        }
        .logo{
            width: 100%;
            height: 125px;
            background: rgba(10,255,150,0.60);
            margin-top: 76px;
        }
    </style>
</head>

<body>
    <div class="all">
        <div style="width: 100%; height: 80px;background: rgba(36,80,154,1.00);display: flex;">
            <h2 style="color: white;margin-left: 50px;">用户与管理员——注册登录</h2>
            <h2 style="color: white;margin-left: 800px;">
                <?
                echo date("Y年m月d日");
                echo "&nbsp&nbsp&nbsp&nbsp";
                date_default_timezone_set("Asia/Shanghai");
                echo  date("h:i:sa");
                ?>
            </h2>
        </div>
    <div class="main" >
        <div class="form" >
        <h1 style="text-align: center">
            <a href="login.php">用户登录</a>
            <a href="register.php" class="current">用户注册</a>
            
        </h1>
        
/*利用表单POST传递注册信息到后端registerPost.php*/

        <form action="registerPost.php" method="post" style="m">
            <table align="center" border="0" style="mar border-collapse: collapse" cellpadding="10" cellspacing="0">
                <tr>
                    <td><b>编号</b></td>
                    <td><input name="id" type="number"></td>
                </tr>
                <tr>
                    <td><b>用户名</b></td>
                    <td><input name="username"></td>
                </tr>
                 <tr>
                    <td><b>密码</b></td>
                    <td><input name="password" type="password"></td>
                </tr>
                 <tr>
                    <td><b>确认密码</b></td>
                    <td><input name="cpassword" type="password"></td>
                </tr>
                <tr>
                    <td><b>性别</b></td>
                    <td>
                        <input name="sex" type="radio" checked value="1">男
                        <input name="sex" type="radio" value="0">女
                    </td>
                </tr>
                  <tr>
                    <td><b>邮箱</b></td>
                    <td>
                        <input name="email" type="text">
                    </td>
                </tr>
                 <tr>
                    <td><b>创建时间</b></td>
                    <td>
                        <input name="createtime" type="date">
                    </td>
                </tr>
            </table>
             <div style="width: 100%;height: 50px;margin-top: 10px;" >
                    
                <button  style="font-size: 20px;margin-left: 20px;margin-top: 5px;background: rgba(5,224,239,1.00);border: 0;box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;border-radius: 30px;width: 100px;height: 50px;" type="submit" value="注册">注册</button>
                <button style="font-size: 20px;margin-left: 50px;background: rgba(5,224,239,1.00);border: 0;box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;border-radius: 30px;width: 100px;height: 50px;" type="reset" value="重置">重置</button>

                
                </div>
        </form>
            </div>
       
    </div>
         <div style="width: 100%; height: 50px;background: rgba(36,80,154,1.00);padding: 1px;">
            <h2 style="color: white;text-align: center;margin-top: 10px;">用户与管理员——注册登录</h2>
        </div>
        
 </div>
</body>
</html>

2、注册后端 registerPost.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
</body>
</html>
<?
    $id=$_POST['id'];
    $username=trim($_POST['username']);
    $password=trim($_POST['password']);
    $cpassword=trim($_POST['cpassword']);
    $sex=$_POST['sex'];
    $email=$_POST['email'];
    $createtime=$_POST['createtime'];

//连接数据库

$conn=mysqli_connect("localhost","root","")or die("连接服务器失败");
mysqli_select_db($conn,"member")or die("选择数据库错误");
mysqli_query($conn,"set names utf8");


//判断是否输入名字
if( ! strlen($username) || ! strlen($password)){
    echo "<script>alert('用户名和密码都必须填写!');history.back();</script>";
    exit();
}

密码和确认密码的验证
if($password <> $cpassword){
    echo "<script>alert('密码和确认密码必须相同!');history.back();</script>";
    exit();
}


//判断用户名是否被占用
$sql="select * from userinfo where username='$username'";
$result=mysqli_query($conn,$sql);
$num=mysqli_num_rows($result);
if($num){
    echo "<script>alert('该用户名已存在,请更换用户名!');history.back();</script>";
    exit();
}
if(!empty($email)){
    if(!preg_match('/^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/',$email)){
      echo "<script>alert('邮箱格式不正确!');history.back();</script>";
      exit();

}
}

$sql="insert into userinfo  values('$id','$username','$password','$email','$sex','$createtime',0)";
$result=mysqli_query($conn,$sql);

if($result){
    echo "<script>alert('注册成功!');history.back();</script>";
}else{
    echo "<script>alert('注册失败!');history.back();</script>";
}

?>

3、登录前端 login.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
    <style>
        .all{
            width: 100%;
            height: 100%;
            align-items: center;
            background: rgba(169,234,240,0.5);
            background-image: url("图片素材/BG.gif");
            background-size: 100% 100%;
            background-repeat: no-repeat;
        }
        .main{
            width: 600px;
            height: 700px;           
            margin:auto;
            background: url("图片素材/dapu.png");
            color: white;
            padding-top: 1px;
            margin-top: 60px;
            margin-bottom: 70px;
      box-shadow: rgba(0, 0, 0, 0.09) 0px 2px 1px, rgba(0, 0, 0, 0.09) 0px 4px 2px, rgba(0, 0, 0, 0.09) 0px 8px 4px, rgba(0, 0, 0, 0.09) 0px 16px 8px, rgba(0, 0, 0, 0.09) 0px 32px 16px;
        }
        .form{
            width: 300px;
            height: 250px;
            background-color:rgba(149,154,246,0.65) ;
            margin-left:160px;
            margin-top: 120px;
            padding: 1px;
            box-shadow: rgba(3, 102, 214, 0.3) 0px 0px 0px 3px;
        }
        .form td{
            font-size: 20px;
            
        }
        h1{
            font-size: 24px;
        }
        h1 a{
            color: navy;text-decoration: none;margin-right: 15px;
        }
        h1 a:last-child{
            margin-right: 0;
        }
        h1 a:hover{
            color: brown;text-decoration: underline
        }
        .current{
            color:brown;
        }
        .logo{
            width: 100%;
            height: 125px;
            background: rgba(10,255,150,0.60);
            margin-top: 176px;
        }
        

    </style>
</head>

<body>
    <div class="all">
        <div style="width: 100%; height: 80px;background: rgba(36,80,154,1.00);display: flex;">
            <h2 style="color: white;margin-left: 50px;">用户与管理员——注册登录</h2>
            <h2 style="color: white;margin-left: 800px;">
                <?

                echo date("Y年m月d日");
                echo "&nbsp&nbsp&nbsp&nbsp";
                date_default_timezone_set("Asia/Shanghai");
                echo  date("h:i:sa");
                ?>
            </h2>
        </div>
    <div class="main" >
        <div class="form" >
        <h1 style="text-align: center;margin-top: 20px;">
            <a href="login.php" class="current">用户登录</a>
            <a href="signup.php" >用户注册</a>
            
        </h1>
        
        <form action="loginPost.php" method="post" style="m" onSubmit="return check()">
            <table class="T" align="center" border="0" style="mar border-collapse: collapse" cellpadding="10" cellspacing="0">
                <tr>
                    <td><b>用户名</b></td>
                    <td><input name="username"></td>
                </tr>
                 <tr>
                    <td><b>密码</b></td>
                    <td><input name="password" type="password"></td>
                </tr>
               
            </table>
             <div style="width: 100%;height: 50px;margin-top: 20px;" >
                    
                <button  style="font-size: 20px;margin-left: 20px;margin-top: 5px;background: rgba(5,224,239,1.00);border: 0;box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;border-radius: 30px;width: 100px;height: 50px;" type="submit" value="登录">登录</button>
                <button style="font-size: 20px;margin-left: 50px;background: rgba(5,224,239,1.00);border: 0;box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;border-radius: 30px;width: 100px;height: 50px;" type="reset" value="重置">重置</button>

                
                </div>
        </form>
            </div>
       
    </div>
         <div style="width: 100%; height: 50px;background: rgba(36,80,154,1.00);padding: 1px;">
            <h2 style="color: white;text-align: center;margin-top: 10px;">用户与管理员——注册登录</h2>
        </div>
        
 </div>
      <script>
        function check(){
            let username = document.getElementsByName('username')[0].value.trim();
            let password = document.getElementsByName('password')[0].value.trim();
            //用户名验证
            
            let passwordReg = /^[a-zA-Z0-9_*]{6,10}$/;
            if(!passwordReg.test(password)){
                alert ('密码必须填写,且只能为大小写字母和数字,以及*,_构成,长度为6-10个字符');
                return false;
            }
            return true;
        }
    </script>

</body>
</html>

4、登录后端 loginPost.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
</body>
</html>
<?
    session_start();

    $username=trim($_POST['username']);
    $password=trim($_POST['password']);


if( ! strlen($username) || ! strlen($password)){
    echo "<script>alert('用户名和密码都必须填写!');history.back();</script>";
    exit();
}
else{

    if(!preg_match('/^[a-zA-Z0-9_*]{6,10}$/',$password)){
    echo "<script>alert('密码必须填写,且只能为大小写字母和数字,以及*,_构成,长度为6-10个字符!');history.back();</script>";
    exit();
}
    
}

$conn=mysqli_connect("localhost","root","")or die("连接服务器失败");
mysqli_select_db($conn,"member")or die("选择数据库错误");
mysqli_query($conn,"set names utf8");

$sql="select * from userinfo where username='$username' and psd='$password'";
$result=mysqli_query($conn,$sql);
$num = mysqli_num_rows($result);
if($num){
    $_SESSION['loginusername']=$username;
    
    $info = mysqli_fetch_array($result);
    if($info['ad']){
<!--判断数据库中ad字段值,为1则判定为管理员,并进入管理员界面,否则则判定为用户,进入用户界面    -->
        echo "<script>alert('欢迎管理员登录!');location.href='datashow.php';</script>";
    }else{
        echo "<script>alert('登录成功!');location.href='home.php';</script>";    
    }
    
}
else{
    unset($_SESSION['loginusername']);

    echo "<script>alert('登录失败!');history.back();</script>";
}

?>