首页 > 技术文章 > PHP实践项目【1】:注册登录页面

sparrow32 2018-08-16 16:57 原文

在我们这个项目里面,一共用到了5个php文件,他们分别是:

login.php 登录页面

logincheck.php 登录检测php文件

register.php 新用户注册页面

regcheck.php 新用户注册检验及插入数据库处理php文件

index.php 登录成功后的显示页面

首先我们来看login.php页面

<!DOCTYPE html>
<html>
 <head>
 <title>用户登录页面</title>
 <meta charset="utf-8" />
    <style>
        h1{
            /*设置标题的上边距为50px*/
            margin-top:50px;
            /*设置标题居中显示*/
            text-align:center;
        }
         div{
            /*设置这个div的宽度*/
             width:300px;
            /*设置这个div的高度*/
             height:150px;
            /*设置这个div的背景颜色*/
             background:#f0f0f0;
            /*设置上边距为150%,左右边距为自适应(居中显示)*/
            margin:50px auto;
            /*加大一下内边距,看起来不那么挤,也可以理解为内容在容器里的缩进*/
            padding:20px;
            /*设置文本居中显示*/
            text-align:center;
         }
        table,td{
            /*设置表格居中显示*/
            margin:auto;
            /*设置表格内边距,看起来不那么挤*/
            padding:5px;
        }
    </style>
 </head>
 <body>
    <!-- -->
     <!-- 登录界面 login.php-->
    <h1>用户登录</h1>
     <div>
        <!-- 创建一个表单,这个表单将会把内容提交给logincheck.php页面,提交方法为post -->
         <form action="logincheck.php" method="post"> 
             <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username" maxLength="6"></td>
                </tr>
                <tr>
                    <td>密 码:</td>
                    <td><input type="password" name="userpwd" maxLength="6"></td>
                </tr>
                <tr>
                    <td><input type="submit" name="submit" value="登陆" > </td>
                    <td><a href="register.php">注册</a></td>
                </tr>
             </table>
         </form>
     </div>
 </body>
</html>

 logincheck.php 文件:

<?php  
//登录处理界面 logincheck.php
//判断是否按下登录按钮
    if(isset($_POST["submit"]) && $_POST["submit"] == "登陆")  
    {  
    //将用户名和密码存入变量中,供后续使用
        $user = $_POST["username"];  
        $pwd = $_POST["userpwd"];  
        if($user == "" || $pwd == "")  
        {
            //用户名或者密码其中之一为空,则弹出对话框,确定后返回当前页的上一页  
            echo "<script>alert('请输入用户名或密码!'); history.go(-1);</script>";  
        }  
        else  
        {  //确认用户名密码不为空,则连接数据库
            $conn = mysqli_connect("localhost","root","root","mydb");
             if(mysqli_errno($conn)){
                echo "连接失败:" . mysqli_errno($conn);
                exit;
             }
             //查询账号密码是否正确
            $sql = "select username,userpwd from user where username = '$user' and userpwd = '$pwd'";  
            //返回查询结果
            $result = mysqli_query($conn,$sql); 
            //查询结果的条数
            $num = mysqli_num_rows($result);  
            //如果查询结果的条数不为0 
            if($num)  
            {  
                echo "<script>alert('成功登录'); window.location.href='index.php';</script>";  
            }  
            else  
            {  
                echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>";  
            }  
        }  
    }  
    else  
    {  
        echo "<script>alert('提交未成功!');</script>";  
    }  
  
?>

register.php 页面:

<!DOCTYPE html>
<html>
 <head>
 <title>用户登录页面</title>
 <meta charset="utf-8" />
    <style>
        h1{
            /*设置标题的上边距为50px*/
            margin-top:50px;
            /*设置标题居中显示*/
            text-align:center;
        }
         div{
            /*设置这个div的宽度*/
             width:300px;
            /*设置这个div的高度*/
             height:150px;
            /*设置这个div的背景颜色*/
             background:#f0f0f0;
            /*设置上边距为150%,左右边距为自适应(居中显示)*/
            margin:50px auto;
            /*加大一下内边距,看起来不那么挤,也可以理解为内容在容器里的缩进*/
            padding:20px;
            /*设置文本居中显示*/
            text-align:center;
         }
        table,td{
            /*设置表格居中显示*/
            margin:auto;
            /*设置表格内边距,看起来不那么挤*/
            padding:5px;
        }
    </style>
 </head>
 <body>
    <!-- -->
     <!-- 注册页面 register.php-->
    <h1>用户注册</h1>
     <div>
        <!-- 创建一个表单,这个表单将会把内容提交给regcheck.php页面,提交方法为post -->
         <form action="regcheck.php" method="post"> 
             <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username" maxLength="12"></td>
                </tr>
                <tr>
                    <td>密 码:</td>
                    <td><input type="password" name="userpwd" maxLength="12"></td>
                </tr>
                <tr>
                    <td>确认密码:</td>
                    <td><input type="password" name="confirm" maxLength="12"></td>
                </tr>    
             </table>
            <input type="submit" name="register" value="注册" > 
         </form>
     </div>
 </body>
</html>

 regcheck.php页面

<?php  
//注册处理界面 regcheck.php

//使用 isset 函数 检测变量是否已设置 并且非 NULL
    if(isset($_POST["register"]) && $_POST["register"] == "注册")  
    {  
        //将 register.php 页面中表单内容赋值给变量
        $user = $_POST["username"];  
        $pwd = $_POST["userpwd"];  
        $pwd_confirm = $_POST["confirm"];  
        
        // check 输入项不能为空
        if($user == "" || $pwd == "" || $pwd_confirm == "")  
        {  
            echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";  
        }  
        else  
        {      //如果两次输入的密码一致,则开始连接数据库
            if($pwd == $pwd_confirm)  
            {  
                //开始连接数据库
                $conn = mysqli_connect("localhost","root","root","mydb");    
                //如果连接数据库发生错误退出
                if(mysqli_errno($conn)){
                    echo mysqli_error($conn);
                    exit;
                }
                
                //查询user数据表的username字段,看是否有与$user相同的记录
                $sql ="select username from user where username = '$user'";
                
                //执行SQL语句,并把结果保存在$result中
                //针对成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,将返回一个 mysqli_result 对象。
                //针对其他成功的查询,将返回 TRUE。
                //如果失败,则返回 FALSE。
                $result = mysqli_query($conn,$sql); 
                
                echo "查询返回的结果类型为" . gettype($result) . '<br>';
                
                if($result){
                    echo "查询成功<br>";
                    //mysqli_num_rows函数返回结果集中行的数量                     
                    $num = mysqli_num_rows($result);
                    echo "查询行数为" . $num . "<br>";
                }else{
                    echo "查询错误!" . mysqli_error($conn);
                }
                
                if($num>0)//如果已经存在该用户  
                {  
                    echo "<script>alert('用户名已存在'); history.go(-1);</script>";  
                }  
                else    //不存在当前注册用户名称就将这些信息插入到数据表中  
                {   
                    $sql_insert = "INSERT INTO user(username,userpwd) VALUES('$user','$pwd')"; 
                    //执行插入语句
                    $res_insert = mysqli_query($conn,$sql_insert);  
                    if($res_insert)  
                    {      //插入成功
                        echo "<script>alert('注册成功!'); history.go(-1);</script>";  
                    }  
                    else  //插入失败显示系统忙
                    {  
                        //echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";  
                        echo "插入 MySQL 失败: " . mysqli_connect_error(); 
                    }  
                }  
            }  
            else  //如果两次输入的密码不一致
            {  
                echo "<script>alert('密码不一致!'); history.go(-1);</script>";  
            }  
        }  
    }  
    else  
    {  
        echo "<script>alert('提交未成功!');</script>";  
    }  
?>

 

推荐阅读