我的任務是使用 php 創建一個登錄系統,特別是一個將密碼散列到 .txt 檔案中的任務(這不是你想的那樣)我已經嘗試過,但無法弄清楚如何甚至開始。有人告訴我用函式 file() 創建一個陣列。將不勝感激任何形式的幫助
目前有這個
<!DOCTYPE html>
<html lang="en">
<body>
<form action="test.php" method="post">
<span>Username</span>
<input type="text" name="Username" id="Login-Input-Username" required><br>
<span>Password</span>
<input type="password" name="Password" id="Login-Input-Password" required>
<button type="submit" name="Submit-Button" value="submit" id="Login-
Button">Sign In</button>
</form>
<?php
if(isset($_POST['Submit-Button']))
{
$username = $_POST['Username'];
$password = $_POST['Password'];
$hash = password_hash($password, PASSWORD_DEFAULT);
$fileRows = file("accounts.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}?>
uj5u.com熱心網友回復:
我真的不知道您為什么要將資料存盤在檔案中,但是您可以將 PHP 代碼重寫為如下所示
<?PHP
if(isset($_POST['Submit-Button']))
{
$username = $_POST['Username'];
$password = $_POST['Password'];
$hash = password_hash($password, PASSWORD_DEFAULT);
$data = [
'username' => $username,
'password' => $hash,
];
$fileRows = file_put_contents('accounts.txt', json_encode($data).PHP_EOL , FILE_APPEND | LOCK_EX);
}?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/460254.html
