一个初学者的留言板(登录和注册)




前言:大一新生一个,把学习内容记录一下,若有疑问或者错误可以留言并指出来,我会再看看的。下面是我写的留言板。东西有点多,分3次发完吧。



一、登录

<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>留言板系统</title>
    <style>
        .main{width: 80%;margin: 0 auto;text-align: center;}
        h2{font-size: 20px}
        h2 a{color: navy;text-decoration: none;margin-right: 15px}
        h2 a:last-child{margin-right: 0}
        h2 a:hover{color: brown;text-decoration: underline}
        .current{color: brown}
        .red{color: red}
        .current{color: brown}
        .logged{font-size: 16px;color: darkgreen}
        .logout{margin-left: 20px;margin-bottom: 15px;}
        .logout a{color: cornflowerblue;text-decoration: none}
        .logout a:hover{text-decoration: underline}
    </style>
</head>
<body  background="5.jpg"
style="background-repeat:no-repeat;
       background-attachment: fixed;
       background-size: 100% 100%;" >
<div class="main">
<h1>留言板系统</h1>
    <h2>
        <a href="登录.php">登录</a>
        <a href="注册.php">注册</a>
    </h2>
    <form action="postlogin.php" method="post" onsubmit="return check()">
        <table align="center" border="1" style="border-collapse: collapse" cellpadding="10" cellspacing="0">
            <tr>
                <td align="right">用户名</td>
                <td align="left"><input name="username"><span class="red">*</span></td>
            </tr>
            <tr>
                <td align="right">密码</td>
                <td align="left">
                    <input type="password" name="pw">
                    <span class="red">*</span>
                </td>
            </tr>
            <tr>
                <td align="right"><input type="submit" value="提交"></td>
                <td align="left">
                    <input type="reset" value="重置">
                </td>
            </tr>
        </table>
    </form>
</div>
<script>
    function check(){
        let username = document.getElementsByName('username')[0].value.trim();
        let pw = document.getElementsByName('pw')[0].value.trim();
        //用户名验证
        let usernameReg = /^[a-zA-Z0-9]{1,10}$/;
        if(!usernameReg.test(username)){
            alert('用户名必填,且只能大小写字符和数字构成,长度为1到10个字符!');
            return false;
        }
        let pwreg = /^[a-zA-Z0-9_*]{1,10}$/;
        if(!pwreg.test(pw)){
            alert('密码必填,且只能大小写字符和数字,以及*、_构成,长度为1到10个字符!');
            return false;
        }
        return true;
    }
</script>
</body>
</html>
<?php
session_start();
$username = trim($_POST['username']);
$pw = trim($_POST['pw']);
include_once "conn.php";
//对用户密码验证
if(!strlen($username) or !strlen($pw) ){
    echo "<script>alert('用户名和密码都必须填写');history.back();</script>";
    exit;
 }
 else{
    if(!preg_match('/^[a-zA-Z0-9]{1,10}$/',$username)){
        echo "<script>alert('用户名必填,且只能大小写字符和数字构成,长度为1到10个字符!');history.back();</script>";
        exit;
    }
 
    if(!preg_match('/^[a-zA-Z0-9_*]{1,10}$/',$pw))
    {
    echo "<script>alert('密码必填,且只能大小写字符和数字,以及*、_构成,长度为1到10个字符!');history.back();</script>";
    exit;
    }
}
$sql = "select * from info where username = '$username' and pw = '" . md5($pw) . "'";
$result = mysqli_query($conn,$sql);
$num = mysqli_num_rows($result);
if($num){
    $_SESSION['loggedUsername']=$username; 
    //判断是不是管理员
    $info = mysqli_fetch_array($result);
    if($info['admin']){
        $_SESSION['isAdmin'] = 1;
    }
    else{
        $_SESSION['isAdmin'] = 0;
    }
    echo "<script>alert('登录成功!');location.href='留言.php';</script>";
}
else{
    unset($_SESSION['isAdmin']);
    unset( $_SESSION['loggedUsername'] );
	echo "<script>alert('登录失败!请重试!');history.back();</script>";
}
?>

第一张图是登录的页面,第二张图是后端处理一下信息(进行一些验证和查询数据库)

二、注册

<html>
<head>
    <meta charset="utf-8">
    <title>用户注册页面</title>
    <style>
        .main{width: 80%;margin: 0 auto;text-align: center;}
        h2{font-size: 20px}
        h2 a{color: navy;text-decoration: none;margin-right: 15px}
        h2 a:last-child{margin-right: 0}
        h2 a:hover{color: brown;text-decoration: underline}
        .current{color: brown}
        .red{color: red}
        .current{color: brown}
        .logged{font-size: 16px;color: darkgreen}
        .logout{margin-left: 20px;margin-bottom: 15px;}
        .logout a{color: cornflowerblue;text-decoration: none}
        .logout a:hover{text-decoration: underline}
    </style>
</head>
<body background="3.jpg">
    <div class="main">
        <h1>留言板系统</h1>
        <?php
        if(isset($_SESSION['loggedUsername']) && $_SESSION['loggedUsername'] <> ''){
        ?>
            <div class="logged">当前登录者:<?php echo $_SESSION['loggedUsername'];?> <?php if($_SESSION['isAdmin']) {?><span style="color: crimson">欢迎管理员登录</span><?php }?> <span class="logout"><span class="logout"><a href="logout.php">注销登录</a></span> </div>
        <?php
        }
        ?>
        <h2>
            <a href="登录.php">登录</a>
            <a href="注册.php">注册</a>
        </h2>
        <form action="index.php" method="post" onsubmit="return check()">
            <table align="center" border="1" style="border-collapse: collapse" cellpadding="10" cellspacing="0">
                <tr>
                    <td align="right">用户名</td>
                    <td align="left"><input name="username" ></td>
                </tr>
                <tr>
                    <td align="right">密码</td>
                    <td align="left"><input type="password" name="pw" ></td>
                </tr>
                <tr>
                    <td align="right">确认密码</td>
                    <td align="left"><input type="password" name="cpw"></td>
                </tr>
                <tr>
                    <td align="right"><input type="submit" value="提交"></td>
                    <td align="left">
                        <input type="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>
    </div>
    
    <script>
        function check(){
            let username = document.getElementsByName('username')[0].value.trim();
            let pw = document.getElementsByName('pw')[0].value.trim();
            let cpw = document.getElementsByName('cpw')[0].value.trim();
        
            //用户名验证
            let usernameReg = /^[a-zA-Z0-9]{1,10}$/;
            if(!usernameReg.test(username)){
                alert('用户名必填,且只能由大小写字符和数字构成,长度为1到10个字符!');
                return false;
            }
            let pwreg = /^[a-zA-Z0-9_*]{1,10}$/;
            if(!pwreg.test(pw)){
                alert('密码必填,且只能大小写字符和数字,以及*、_构成,长度为1到10个字符!');
                return false;
            }
            else{
                if(pw!=rpw){
                    alert('密码和确认密码必须相同!')
                return false;
                }
            }
        }
    </script>
</body>
</html>

<?php
header("Content-Type:text/html;charset=utf-8");
//在后端获取前端表单数据的方法是使用全局数组$_GET或$_POST
$username = trim($_POST['username']);
$pw = trim($_POST['pw']);
$cpw =trim( $_POST['cpw']);


include_once "conn.php";

//验证
if(!strlen($username) or !strlen($pw) ){
    echo "<script>alert('用户名和密码都必须填写');history.back();</script>";
    exit;
 }
 else{
    if(!preg_match('/^[a-zA-Z0-9]{1,10}$/',$username)){
        echo "<script>alert('用户名必填,且只能大小写字符和数字构成,长度为1到10个字符!');history.back();</script>";
        exit;
    }
 }
 if($pw <> $cpw){
    echo"<script>alert('密码和确认密码必须相同');history.back();</script>";
    exit;
 }
 else{
    if(!preg_match('/^[a-zA-Z0-9_*]{1,10}$/',$pw))
        {
        echo "<script>alert('密码必填,且只能大小写字符和数字,以及*、_构成,长度为1到10个字符!');history.back();</script>";
        exit;
        }
    }
   //判断用户名是否被占用
$sql = "select * from info where username = '$username'";
$result = mysqli_query($conn,$sql);  //返回一个记录集
$num = mysqli_num_rows($result);
if($num){
    echo "<script>alert('此用户名已经被占用了,请返回重新输入');history.back();</script>";
    exit;
}
 

//sql语句
$sql = "insert into info (username,pw) values ('$username','" .md5($pw). "')";
$result = mysqli_query($conn,$sql);
if($result){
    echo "<script>alert('注册成功,点击确认重新登录。');location.href='登录.php'</script>";
}
else{
    echo "<script>alert('数据插入失败');history.back();</script>";
}




图一是注册的页面和图二是后端验证与连接数据库。

三、用户数据库

 总结:

登录和注册原理差不多吧,学一下数据库和一点php还有怎么写页面就可以了。可以把登录注册当成一个实战写一下。  留言,查看留言和登录,注册挺像的。就不多写了。下一次写管理员的东西。



  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值