PHP实现一个简陋的注册登录页面

PHP实现一个简陋的注册登录页面

今天来水一篇没有**用的 /滑稽脸,代码简陋臃肿考虑不全,各位大佬轻喷,还望不吝赐教。
首先考虑了一下需要至少四个页面:register.htmlregister.phplogin.htmllogin.php
register.html是这么写的:

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
    <title>注册界面</title>
</head>
<body>
    <form action="register.php" method="post">
        用户名:<input type="text" name="name"></input>
        <br />
        密码:<input type="password" name="password"></input>
        <br />
        <input type="submit" value="注册"></input>
    </form>
</body>
</html>

register.php是这么写的:

<?php
    header("Content-type:text/html;charset=utf-8");
    $conn=new mysqli('localhost','wy','000000','test');
    if ($conn->connect_error){
        die("服务器连接失败!");
    }
    $name=$_POST["name"];
    $password=$_POST["password"];
    $sql="insert into new_info values('$name',$password)";
    $res=$conn->query($sql);
    if(!$res){
        echo "注册失败!";
    }else{
            if($conn->affected_rows>0){
                echo "注册成功!";
            }else{
                echo "注册失败";
            }
    }
    $conn->close();
?>

login.html是这么写的:

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
    <title>登录界面</title>
</head>
<body>
    <form action="login.php" method="post">
        用户名:<input type="text" name="name"></input>
        <br />
        密码:<input type="password" name="password"></input>
        <br />
        <input type="submit" value="登录"></input>
    </form>
</body>
</html>

login.php是这么写的:

<?php
    header("Content-type:text/html;charset=utf-8");
    $conn=new mysqli('localhost','wy','000000','test');
    if ($conn->connect_error){
        die("服务器连接失败!");
    }
    $name=$_POST["name"];
    $password=$_POST["password"];
    $sql_name="select name from new_info where name='$name'";
    $res_sql=$conn->query($sql_name);
    if($conn->affected_rows==0){
        die("账号或密码有误");
    }else{
        $sql_pass="select password from new_info where name='$name'";
        $res_pass=$conn->query($sql_pass);
        $row_pass=$res_pass->fetch_row();
        if($row_pass[0]==$password){
            echo "登录成功!";
        }else{
            echo "账号或密码有误";
        }
    }
    $conn->close();
?>

然后来看一下效果:

看一下数据库:

可以看到已经将数据写入数据库。
接着来登录试一下:

换个错误密码试一下:

猜你喜欢

转载自www.cnblogs.com/Timesi/p/9349646.html