DAY6:利用 PHP 编写登陆页面

这里代码直接放出了,太懒了,就不细致详解了

这里创建几个 php 文件

1、代码展示

图中蓝底的文件,这里放出其中的代码(login.php、signup.php、conn.php)

1.1、login.php

<?php
    error_reporting(0);
    session_start();
    $postUsername =isset($_POST['username'])? $_POST['username'] : '';//用户名
    $postpassword =isset($_POST['password'])? $_POST['password'] : '';//密码
    $conn = mysqli_connect("127.0.0.1","root","admin123", "regist") or die("数据库连接出错!");//相应的数据库地址 用户名 和密码
    $sql ="SELECT username,password1 FROM users WHERE username ='$postUsername'";
    $query =mysqli_query($conn,$sql);
    $row =mysqli_fetch_array($query,MYSQLI_ASSOC);
    $username =isset($row['username'])? $row['username'] : '';
    $password =isset($row['password1'])? $row['password1'] : '';
    $_SESSION["username"]=$postUsername;
    if(isset($_POST['login']))
    {
        if($username ==$postUsername && $password==$postpassword)
        {    
            echo"<script>alert('登录成功');window.location ='1.php'</script>";
        }
        else
        {
             echo"<script>alert('用户名或密码错误!');history.go(-1)</script>";
        }
    }
    
    $user;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
</head>
<body>
    <h2>登陆页面</h2>
    <form action ="" method = "post" name ="myform" ousubmit ="return Checked();">
       <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
              <td><input type="submit" name="login" value="登录"></td>
              <td><a href ="signup.php">注册</a></td>
            </tr>
            <tr>
               <td><a href="7.html">返回首页</a></td>
            </tr>
        </table>
    </form>
</body>
</html>

因为之前写的有些问题,调试时将 include() 函数注释了,所以这个文件并没有使用 include(conn.php);

1.2、signup.php

<?php
    session_start();
    include("conn.php");//连接数据库
    $username =isset($_POST['username'])? $_POST['username'] : '';//用户名
    $password2 =isset($_POST['password2'])? $_POST['password2'] : '';//密码
    $password1 =isset($_POST['password1'])? $_POST['password1'] : '';
 
if(isset($_POST['signup']))
{
    if($password2==$password1)
    {
        $sql ="INSERT INTO users VALUE('$username','$password2');";
        mysqli_query($conn,$sql);
        echo "<script>alert('注册成功');window.location ='login.php';</script>";
    }
    else
    {
        echo "<script>alert('两次密码不一致!');window.location ='login.php';</script>";
    }
}
 
 
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>注册页面</h2>
    <form action ="" method = "post" name ="myform" ousubmit ="return Checked();">
       <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password1"></td>
            </tr>
            <tr>
                <td>确认密码:</td>
                <td><input type="password" name="password2"></td>
            </tr>
            <tr>
              <td><input type="submit" name="signup" value="注册"></td>
              <td><a href ="login.php">登录</a></td>
            </tr>
            <tr>
               <td><a href="7.html">返回首页</a></td>
            </tr>
        </table>
    </form>
</body>
</html>

1.3、conn.php

<?php
   $conn = mysqli_connect("127.0.0.1","root","admin123") or die("数据库连接出错!");//相应的数据库地址 用户名 和密码
   $selected = mysqli_select_db($conn,"regist");
?>

关键地方已经注释,一些重要函数可以在菜鸟教程中搜索

2、展示图

登陆界面

注册界面

返回首页

更多推荐

DAY6:利用 PHP 编写登陆页面