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