亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過文本文件而不是授權文件驗證憑據?

如何通過文本文件而不是授權文件驗證憑據?

PHP
忽然笑 2022-06-17 11:00:32
我有一個帶有基本表單的 PHP 登錄腳本,它導致這個文件是包含憑據的授權文件,但我希望這個文件檢查文本文件中匹配的用戶名和密碼,以從格式為之類的文本文件中獲取匹配的用戶名和username:password密碼相似的。有人有什么想法嗎?<?phpif ( ! isset( $_POST['submitted'] ) )header('Location: ' . $_SERVER['HTTP_REFERER']);$credentials = [   'username' => 'TESTUSER',   'password' => 'TESTPASS'];if ( $credentials['username'] !== $_POST['username']       OR $credentials['password'] !== $_POST['password'] ){        header('Location: ' . $_SERVER['HTTP_REFERER']);    exit();}session_start();$_SESSION["000000"] = "1"; header('Location:' . 'index.php');exit();
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

持久存儲類型的選擇取決于您,但您應該:


切勿將密碼存儲為純文本,password_hash()在存儲之前使用。


然后,在登錄時,用于password_verify()驗證密碼是否與存儲介質(例如數據庫/平面文件)中的哈希匹配。


例子:


<?php

echo password_hash("somepassword", PASSWORD_DEFAULT);


$hash = '$2y$10$f.iC/tadtwSws25fW2zRg./.xlY.mRK82Ys9M4acbPU/b614vA1vy';


if (password_verify('somepassword', $hash)) {

    echo 'The password is valid!';

} else {

    echo 'The password is not valid';

}

你可以玩這個演示


更新:平面文件 ( .json) 用戶存儲/登錄驗證腳本的簡單示例。您仍然需要對用戶輸入進行數據驗證和清理,并確定平面文件存儲是否是最佳解決方案/是否足以滿足您的應用程序所需的安全級別。


有兩個文件:


index.php應用程序 - 用戶商店/登錄驗證

users.json平面文件數據庫(用戶憑據:name和password)

index.php呈現兩種形式,第一種可用于添加用戶users.json,第二種用于登錄驗證。


index.php


<?php

function getForm(string $submitName, string $submitValue)

{

    $form = <<<HEREDOC

    <form method="POST">

    <label for="username">User Name : </label>

    <input type="text" name="username" id="username" required>

    <label for="password">Password : </label>

    <input type="text" name="password" id="password" required>

    <input type="submit" name="$submitName" value="$submitValue">

    </form>

HEREDOC;

    return $form;

}

// build forms

$userForm = getForm('submit_user', 'Add User');

$loginForm = getForm('submit_login', 'Login');


/* add a new user to flat file database */

echo $userForm;

if (isset($_POST['submit_user'])) {

    // retrieve user input - you still need to do data validation and sanitizing

    $userName = (isset($_POST['username'])) ? $_POST['username'] : null;

    $passWord = (isset($_POST['password'])) ? $_POST['password'] : null;

    $passWord = password_hash($passWord, PASSWORD_DEFAULT); // store a hash

    // get user.json file

    $file = "./users.json";

    $users = json_decode(file_get_contents($file), true);

    // insert new user credentials

    $users['users'][] = ['name' => $userName, 'password' => $passWord];

    // write  to flat file database

    file_put_contents($file, json_encode($users));

}

/* login - verify user credentials */

echo $loginForm;

if (isset($_POST['submit_login'])) {

    // retrieve user input - you still need to do data validation and sanitizing

    $userName = (isset($_POST['username'])) ? $_POST['username'] : null;

    $passWord = (isset($_POST['password'])) ? $_POST['password'] : null;


    // get user.json file

    $file = "./users.json";

    $users = json_decode(file_get_contents($file), true);


    // verify user

    foreach ($users['users'] as $key => $value) {

        if (strtolower($value['name']) === strtolower($userName)) {

            $hash = $value['password'];

            $verify = password_verify($passWord, $hash); // verify

            if ($verify === true) {

                echo 'User Login Validated';

            } else echo 'Login Not Valid';

        }

    }

}

平面文件用戶數據庫:users.json


{

  "users": [

    {

      "name": "Jack",

      "password": "$2y$10$FBLkEDGX3I6HAVgptJ6q1ujo5K6cFtZn2wNKXKUhoWGNtcwfsRlpi"

    },

    {

      "name": "Jill",

      "password": "$2y$10$yKp79.HujKW3yFvxPDYvqePcUJ9uLWJ92d5TpSy62YtuRTezWrsna"

    },

    {

      "name": "Annie",

      "password": "$2y$10$eWctVmNAadkf138J0iTVr.5u7vmRl9vcglAhSEjbp0WqQphKFjwYC"

    }

  ]

}


查看完整回答
反對 回復 2022-06-17
  • 1 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號