PHP项目:实现用户登录与数据库连接

实战项目:

  • 实现账号密码界面的登录
  • 实现个人信息的头像上传
  • 实现数据库的连接。
  • 使用Cookie

开始界面

在这里插入图片描述

<html>
<head>
    <meta charset="utf-8">
    <title>首页-论坛项目</title>
</head>
<?php
if(isset($_COOKIE['name'])){
    echo "欢迎您<a href=./member/>个人中心</a>".$_COOKIE['name'];
}else{
}
?>
<body>
    <h1>Welcome to my Forum!</h1>
    <a href='.\member\register.html'>注册</a>
    <a href='.\member\login.php'>登录</a>
    <hr />
</body>

</html>

注册界面

  • 判断密码是否输入不一致
  • 将数据导入到数据库
  • 页面跳转
<meta charset="utf-8">

<?php
include "../inc/dblink.inc.php";
?>
<?php
//var_dump(($_POST));
echo "<hr/>";
if (isset($_POST['usersubmit'])) {
    $username = $_POST['user'];
    $userpass1 = $_POST['passwd1'];
    $userpass2 = $_POST['passwd2'];
    if (
        isset($username) && isset($userpass1) &&
        isset($userpass2) && $userpass2 === $userpass1
    ) {
        $sql = "insert into users(name,password) values('" . $username . "','" . md5($userpass1) . "')";
        if (!mysqli_query($link, $sql)) {
            die(mysqli_error($link));
        }else{
            echo "注册成功!请去往<a href='index.php'>个人中心</a>";
            setcookie("name",$username);
        }
    } else {
        echo "注册信息有误,请重新<a  href='register.html'>注册</a>";
    }
} else {
    header("Location:register.html");
}

?>

<?php
mysqli_close($link);
?>

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

个人中心:

  • 注销
  • 头像上传
  • 显示数据库信息

在这里插入图片描述

<meta charset="utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_COOKIE['name'])) {
    echo "欢迎您," . $_COOKIE['name'];
    $username=$_COOKIE['name'];
    $sql="select * from users where name='".$username."'";
    if($results=mysqli_query($link,$sql)){
        if(mysqli_num_rows($results)>0){
            $result=mysqli_fetch_assoc($results);
            echo '<hr/>';
            echo "欢迎  ________ <a href='logout.php'>注销</a>";
            echo '<hr/>';
            echo "您的头像是<img src='".$result['photo']."'>";
            echo "<a href='update.php'>修改头像</a>";
            echo '<hr/>';
            echo "账户余额: ".$result['money']."<span style='color:red;'>请联系管理员</span>";
        }else{

        }
    }
} else {
    echo "<a href='login.php'>请登录</a>";
}
?>
<html>

<head>
    <meta charset="utf-8">
    <title>首页-个人中心</title>
    <h1>首页-个人中心</h1>
</head>

<body>
    <h2>论坛</h2>

</body>



</html>

注销操作

  • 删除Cookie
<meta charset="utf-8">

<?php
setcookie('name', $_COOKIE['name'], time() - 99999, "/p/do/");
echo "注销成功<a href='index.php'>返回个人中心</a>";

?>

上传头像;

  • submit提交头像
  • 保存到数据库
<meta charset="utf-8">
<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_POST['usersubmit'])) {
    $username = $_COOKIE['name'];
    $tmp_path = $_FILES['up']['tmp_name'];
    $path ="./".$_FILES['up']['name'];
    //echo $path;
    if (move_uploaded_file($tmp_path, $path)) {
        $path = mysqli_real_escape_string($link, $path);
        $sql = "update users set photo='" . $path . "' where name='" . $username . "'";
        //echo $sql;
        if (mysqli_query($link, $sql)) {
            echo "图片上传成功,返回<a href='index.php'>个人中心</a>";
        } else {
            die(mysqli_error($link));
        }
    } else {
        echo "图片上传失败!";
    }
} else {
    $htm = <<<HTML
<meta charset="utf-8">
<form method="POST" enctype="multipart/form-data">
<input type="file" name="up"></br>
<input type="submit" name="usersubmit"  value="submit">

</form>
HTML;
echo $htm;
}


?>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

登录界面

  • 查询数据库信息
  • 判断用户是否存在

在这里插入图片描述
在这里插入图片描述

<?php
include "../inc/dblink.inc.php";
?>
<?php
if (isset($_POST['usersubmit'])) {
    $username = $_POST['username'];
    $password = $_POST['passwd'];
    $sql = "select * from users where name='" . $username . "' and password='" . md5($password) . "'";
    if ($results = mysqli_query($link, $sql)) {
        if (mysqli_num_rows($results) > 0) {
            setcookie('name',$username,time()+2000);
            echo "登录成功<a href='index.php'>返回个人中心</a>";
        } else {
            echo "用户名和密码输入错误,请<a href='login.php'>请重新登录</a>";
        }
    } else {
        die(mysqli_error($link));
    }
} else {
    $html = <<<HTML
<form method="POST">
    User: <input type="text" name="username"></br>
    Password: <input type="password" name="passwd"></br>
    <input type="submit" name="usersubmit" value="submit">
</form>
HTML;
}
echo $html;
?>

<?php

mysqli_close($link);
?>


posted @ 2021-08-10 18:53  Zeker62  阅读(700)  评论(2编辑  收藏  举报