Skip to content
Advertisement

How do I make a simple login system using php? [closed]

I got a task to make a login system using php, specifically one that hashes the password into a .txt file (this isnt how you’re suppose to ik that) I’ve tried and tried but can’t figure out how to even start. I’ve been told to Make an array with the function file(). Would appreciate any kind of help

Have this currently

 <!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);
    
    }?>

Advertisement

Answer

I don’t really know why you want to store data in a file but you can rewrite your PHP code to look like this

<?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);    
    }?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement