PHP+MYSQL 实现简单的登录


index.html

登录框:使用HTML、CSS、DIV 通过POST方式把用户名、密码提交给login.php处理。

 

 

<!--登录-->
<div class="login_box">
    <form action="login.php" method="post">
        <h3 class="login_font1">账号登录</h3>
        <div><label class="login_font_user">用户名</label></div>
        <input class="login_user_box" type="text" name="username" placeholder="请输入用户名">
        <div><label class="login_font_pwd">密码</label></div>
        <input class="login_pwd_box" type="password" name="password" placeholder="请输入密码">
        <div><input class="login_submit" type="submit" value="登录"></div>
        <div class="copyright"><a class="copyright_mail" href ="mailto:tech_li@126.com">&copy; 2019 保留所有权利</a></div>
    </form>
</div>

 

通过mysqli扩展库使用面向对象的方式连接数据库,选择库。

connect.php

<?php
    $server="localhost";
    $db_username="root";
    $db_password="xiaoqiang123";
    $dbname="test";
    $conn = new mysqli($server,$db_username,$db_password,$dbname);
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
?>

 

包含connect.php,获取index.html的表单数据,查询数据库是否有正确的数据库条目,根据结果进行返回。

<?php

    header("Content-Type: text/html; charset=utf8");

    include('connect.php');
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    if ($username && $password){//如果用户名和密码都不为空
        $sql = "select * from user where username = '$username' and password=md5('$password')";
        $result = $conn->query($sql);
        $rows=$result->num_rows;

        if($rows){//0 false 1 true
            header("refresh:0;url=welcome.html");//如果成功跳转至welcome.html页面
            exit;
        }else{
            //echo "用户名或密码错误";
            echo "<script>alert('用户名或密码错误!')</script>";
            echo "
                    <script>
                            setTimeout(function(){window.location.href='index.html';},1000);
                    </script>
                ";//如果错误使用js 1秒后跳转到登录页面重试;
        }
    }
    //释放结果集
    $result->free();
?>

 

welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>welcome</title>
</head>
<body>
登录成功

</body>
</html>

 

Mysql操作非常简单,根据自己需求建立相应的库和表项即可,根据需求进行调整。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM