php实现登录注册功能

PHP连接MySQL数据库实现用户的登录与注册功能

1.建立数据库

运用PHP连接MySQL数据库实现登录注册功能并将用户登录日志写入数据库,首先需要在数据库中建立需要的数据库 php 以及用户表 User和日志表 Logs。

User表结构

这里写图片描述

Logs表结构

这里写图片描述

数据库表建成

这里写图片描述

2.代码实现

注册页面register.php

<!DOCTYPE html>
<html>
<head>
<title>注册</title>
<meta name="content-type"; charset=UTF-8">
</head>
<body>
    <div class="content" align="center">
        <!--头部-->
        <div class="header">
        <h1>注册页面</h1>
        </div>
        <!--中部-->
        <div class="middle">
            <form action="registeraction.php" method="post">
                <table border="0">
                    <tr>
                        <td>用户名:</td>
                        <td><input type="text" id="id_name" name="username" required="required"></td>
                    </tr>
                    <tr>
                        <td>密&nbsp;&nbsp;&nbsp;码:</td>
                        <td><input type="password" id="password" name="password" required="required"></td>
                    </tr>
                    <tr>
                        <td>重复密码:</td>
                        <td><input type="password" id="re_password" name="re_password" required="required"></td>
                    </tr>
                    <tr>
                        <td>性别:</td>
                        <td>
                            <input type="radio" id="sex" name="sex" value="男"><input type="radio" id="sex" name="sex" value="女"></td>
                    </tr>
                    <tr>
                        <td>QQ:</td>
                        <td><input type="text" id="qq" name="qq" required="required"></td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td><input type="email" id="email" name="email" required="required"></td>
                    </tr>
                    <tr>
                        <td>电话:</td>
                        <td><input type="text" id="phone" name="phone" required="required"></td>
                    </tr>
                    <tr>
                        <td>地址:</td>
                        <td><input type="text" id="address" name="address" required="required"></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center" style="color:red;font-size:10px;">
                        <!--提示信息-->
                            <?php
                                $err=isset($_GET["err"])?$_GET["err"]:"";
                                switch($err) {
                                    case 1:
                                    echo "用户名已存在!";
                                    break;
                                    case 2:
                                    echo "密码与重复密码不一致!";
                                    break;
                                    case 3:
                                    echo "注册成功!";
                                    break;
                                }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" id="register" name="register" value="注册">
                            <input type="reset" id="reset" name="reset" value="重置">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            如果已有账号,快去<a href="login.php">登录</a>吧!
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <!--脚部-->
        <div class="footer">
        <small>Copyright &copy; 版权所有·欢迎翻版
        </div>
    </div>
</body>
</html>

注册处理页面 registeraction.php

<?php
    //声明变量

    $username = isset($_POST['username'])?$_POST['username']:"";
    $password = isset($_POST['password'])?$_POST['password']:"";
    $re_password = isset($_POST['re_password'])?$_POST['re_password']:"";
    $sex = isset($_POST['sex'])?$_POST['sex']:"";
    $qq = isset($_POST['qq'])?$_POST['qq']:"";
    $email = isset($_POST['email'])?$_POST['email']:"";
    $phone = isset($_POST['phone'])?$_POST['phone']:"";
    $address = isset($_POST['address'])?$_POST['address']:"";

    if($password == $re_password) {
        //建立连接
        $conn = mysqli_connect('localhost','root','123','php');
        //准备SQL语句,查询用户名
        $sql_select="SELECT username FROM User WHERE username = '$username'";
        //执行SQL语句
        $ret = mysqli_query($conn,$sql_select);
        $row = mysqli_fetch_array($ret);
        //判断用户名是否已存在
        if($username == $row['username']) {
            //用户名已存在,显示提示信息
            header("Location:register.php?err=1");
        } else {

            //用户名不存在,插入数据
            //准备SQL语句
            $sql_insert = "INSERT INTO User(username,password,sex,qq,email,phone,address) VALUES('$username','$password','$sex','$qq','$email','$phone','$address')";
            //执行SQL语句
            mysqli_query($conn,$sql_insert);
            header("Location:register.php?err=3");
        }

        //关闭数据库
        mysqli_close($conn);
    } else {
        header("Location:register.php?err=2");
    }

?>

登录页面login.php

<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta name="content-type"; charset=UTF-8">
</head>
<body>
    <div class="content" align="center">
        <!--头部-->
        <div class="header">
        <h1>登录页面</h1>
        </div>
        <!--中部-->
        <div class="middle">
            <form id="loginform" action="loginaction.php" method="post">
                <table border="0">
                    <tr>
                        <td>用户名:</td>
                        <td>
                            <input type="text" id="name" name="username" required="required"
                            value="<?php echo isset($_COOKIE["wang"])?$_COOKIE["wang"]:"";?>">
                        </td>
                    </tr>
                    <tr>
                        <td>密&nbsp;&nbsp;&nbsp;码:</td>
                        <td><input type="password" id="password" name="password"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="checkbox" name="remember"><small>记住我
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center" style="color:red;font-size:10px;">
                        <!--提示信息-->
                            <?php
                                $err=isset($_GET["err"])?$_GET["err"]:"";
                                switch($err) {
                                    case 1:
                                    echo "用户名或密码错误!";
                                    break;
                                    case 2:
                                    echo "用户名或密码不能为空!";
                                    break;
                                }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" id="login" name="login" value="登录">
                            <input type="reset" id="reset" name="reset" value="重置">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            还没有账号,快去<a href="register.php">注册</a>吧!
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <!--脚部-->
        <div class="footer">
        <small>Copyright &copy; 版权所有·欢迎翻版
        </div>
    </div>
</body>
</html>

登录处理页面 loginaction.php

<?php
    //声明变量
    $username = isset($_POST['username'])?$_POST['username']:"";
    $password = isset($_POST['password'])?$_POST['password']:"";
    $remember = isset($_POST['remember'])?$_POST['remember']:"";

    //判断用户名和密码是否为空
    if(!empty($username)&&!empty($password)) {
        //建立连接
        $conn = mysqli_connect('localhost','root','123','php');
        //准备SQL语句
        $sql_select = "SELECT username,password FROM User WHERE username = '$username' AND password = '$password'";
        //执行SQL语句
        $ret = mysqli_query($conn,$sql_select);

        $row = mysqli_fetch_array($ret);

        //判断用户名或密码是否正确
        if($username==$row['username']&&$password==$row['password']) {
            //选中“记住我”
            if($remember=="on") {
                //创建cookie
                setcookie("wang", $username, time()+7*24*3600);
            }
            //开启session
            session_start();
            //创建session
            $_SESSION['user']=$username;
            //写入日志
            $ip = $_SERVER['REMOTE_ADDR'];
            $date = date('Y-m-d H:m:s');

            $info = sprintf("当前访问用户:%s,IP地址:%s,时间:%s \n",$username, $ip, $date);
            $sql_logs = "INSERT INTO Logs(username,ip,date) VALUES('$username','$ip','$date')";

            //日志写入文件,如实现此功能,需要创建文件目录logs
            $f = fopen('./logs/'.date('Ymd').'.log','a+');

            fwrite($f,$info);
            fclose($f);

            //跳转到loginsucc.php页面
            header("Location:loginsucc.php");
            //关闭数据库
            mysqli_close($conn);
        }else {
            //用户名或密码错误,赋值err为1
            header("Location:login.php?err=1");
        }
    }else {
        //用户名或密码为空,赋值err为2
        header("Location:login.php?err=2");
    }

?>

登录成功页面 loginsucc.php

<!DOCTYPE html>
<html>
<head>
<title>登录成功</title>
<meta name="content-type"; charset=UTF-8">
</head>
<body>
    <div>
        <?php
            //开启session
            session_start();
            //声明变量
            $username= isset($_SESSION['user'])?$_SESSION['user']:"";
            //判断session是否为空
            if(!empty($username)){
        ?>
            <h1>登录成功!</h1>
                欢迎您!
        <?php
            echo $username;    
        ?>
            <br/>
            <a href="logout.php">退出</a>
        <?php
            }else {
            //未登录,无权访问
        ?>
            <h1>你无权访问!!!</h1>
        <?php   
            }
        ?>  
    </div>
</body>
</html>

退出页面logout.php

<?php
    //开启session
    session_start();
    //撤销session
    session_unset();
    session_destroy();
    //跳转到login.php
    header("Location:login.php");
?>

运行截图

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值