簡體   English   中英

使用 PDO 函數通過 MySQL 服務器驗證用戶名/密碼

[英]Verifying username/password via MySQL server using PDO functions

我在使用 PDO 功能正確連接到 MySQL 服務器時遇到了一些問題。 我無法從數據庫中查詢我需要的內容,也不太確定要使用哪些 PDO 函數。 因此,我得到 0。 我希望驗證我通過數據庫輸入的密碼和用戶名,並創建一個 if 語句,如果信息正確則啟動會話。

這是我的更新代碼:

<?php
// if a value is given
if (isset($_POST['username' && 'password'));
{

    // starts the session created if login info is correct
    session_start();

    // connectivity to MySQL server
    $db = new PDO('mysql:host=host;dbname=name', 'username', 'password');

    // information entered in form made into a variable
    $username = PDO::quote($_POST['username']); 
    $password = PDO::quote($_POST['password']);

    // after pressing login, checking if the variables exist in the database
    if ($_POST['button'] == 'Login')
    {
        $query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
        $query->bindValue(':username', $username, PDO::PARAM_STR);
        $query->bindValue(':password', $password, PDO::PARAM_STR);
        $query->execute();

        // Check the number of rows that match the SELECT statement 
        if($query = $db->fetch(PDO::FETCH_OBJ)) 
        {
            echo "No records found";
        }
        else
        {
            header("Location: members.php");
            $_SESSION['username'] = $_POST['username'];
        }
    }   
    // After pressing register, stores variable and adds it in register field
    else if ($_POST['register'] == 'Register')
    {
        header("Location: register.php");
        $_SESSION['usernamereg'] = $_POST['username'];
    }

    // If no button is pressed, send to index
    else
    {
        header("Location: http://www.gjertgjersund.com");
    }

// closes the if value is given statement
}
?>

<!DOCTYPE html>
<html>
<head>
    <title> Folder </title>
    <link rel="stylesheet" type="text/css" href="frontpage.css">
</head>
<body>

    <div id="box">
    <div id="wrap">


        <center>
        <img src="./img/logo.png" alt="logo">

        <form action='index.php' method='POST' autocomplete='off'>

        <div class="usernameform">
        <input type='text' name='username' style='border: none; font-size: 20px;'>
        </div>

        <br />

        <div class="passwordform">
        <input type='password' name='password' style='border: none; font-size: 20px;'>
        </div>

        <br />

        <div class="registerlogin">
        <input type='submit' name='button' value='Login' class='input'>
        <input type='submit' name='register' value='Register' class='inputtwo'>
        </div>

        </form>
        </center>

    </div>
    </div>

</body>
</html>

這是一個非常好的問題,部分原因是它展示了許多糟糕的做法,遺憾的是,這些做法至今仍存在,距發布該問題已近十年。 讓我們整理一下

聯系

我在使用 PDO 功能正確連接到 MySQL 服務器時遇到了一些問題。

這是一個公平的問題,沒有任何地方連接只是一行代碼。 М任何重要的選項都必須設置,請參閱我寫的規范連接示例 您可以將連接代碼存儲在單獨的文件中,然后將其包含在您的腳本中。

密碼散列

你永遠不應該在你的數據庫中存儲簡單的密碼。 取而代之的是,必須對密碼進行哈希處理,並使用專門為此目的創建的函數 - password_hash()

然后,要驗證密碼,您必須使用password_verify()

在您正確散列密碼之前,不要再進一步。 請注意,散列密碼的字段大小必須為 60 個字符長。

驗證用戶名和密碼的代碼

最后,現在我們可以編寫代碼了。 它可以簡單得多,只需幾行。 實際上,只需要一個條件。 我們不應該提供有關是否未找到登錄名或用戶名的任何詳細信息 - 只是“登錄名和密碼不匹配”。 所以這里是這樣的:

<?php
if (isset($_POST['username']))
{
    // get the PDO instance
    include 'pdo.php';

    // getting the record for the given username
    $stmt = $pdo->prepare("SELECT * FROM login WHERE username=?");
    $stmt->execute([$_POST['username']]);
    $user = $stmt->fetch();
    // verifying the password
    if ($user && password_verify($_POST['password'], $user['password']))
    {
        // starts the session created if login info is correct
        session_start();
        $_SESSION['username'] = $user['username'];
        header("Location: members.php");
        exit;
    } else {
        $error = "Login and password don't match";
    }
}

在這里,我們正在檢查是否存在這樣的用戶以及密碼是否在單個條件下匹配。

您不必在准備好的語句中使用'並且$username是字符串而不是整數

$query = $db->query("SELECT * FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
 //{ remove it

您的if條件錯誤更改

// if a value is given
if (isset($_POST['username' && 'password'));
{

// if a value is given
if (isset($_POST['username']) and isset($_POST['password']))
{

鍵盤

某些數據庫可能會返回 SELECT 語句返回的行數。 但是,並不能保證所有數據庫都具有這種行為,因此不應依賴於手冊 您可以在以下查詢中使用COUNT(*)fetchColumn()來模擬rowCount()

$query = $db->query("SELECT COUNT(*) FROM login WHERE username=:username AND password=:password");
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':password', $password, PDO::PARAM_STR);
$query->execute();
    // Check the number of rows that match the SELECT statement 
    if($query->fetchColumn() == 0) {
        echo "No records found";
     }else{
            //CODE FOR Success 
            //Etc
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM