DevZ
DevZ

Reputation: 3

How to manually assign user passwords via PHP file

I am a new user and I have a simple question. How do I set a password for a user to log in to my site with PHP? I have no MySQL database just a iOS code/host app. I know this isn’t the most secure option, but only me and my friends are on the site so it doesnt matter. I tried the following:

$password = [
           'john'   => 'password1',
           'paul'   => 'password2',
           'george' => 'password3',
           'robert'  => 'password4'
        ];
        
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $pass = $password[ $_POST['username'] ];
    if (password_verify($_POST['password'], $pass)) {
        echo "Log in successful<br>";
    }
    else {
        echo "Invalid log in<br>";
    }
}

This is my HTML login form code for reference:

<body>    
    <center> <h1>Login</h1> </center>   
    <form>
        <div class="container">   
            <label>Username:</label>   
            <input type="text" placeholder="Enter Username" name="" required>  
            <label>Password:</label>   
            <input type="password" placeholder="Enter Password" name="" required>  
            <button type="submit">Login</button>   
            <input type="checkbox" checked="checked"> Remember me   
            <button type="button" class="cancelbtn"> Cancel</button>   
            <a href="">Forgot password?</a>
        </div>   
    </form>
</body>     
</html>

But it didn’t work… So any help is appreciated.

Upvotes: 0

Views: 50

Answers (1)

John Doe
John Doe

Reputation: 1424

I hope you are in best of health. There are lots of issues in your code. I am adding some points.

  1. You did not set username and password as names in your input fields.
  2. You are using POST method in your PHP code but on HTML your form's method attribute is missing.
  3. Most important, the way you are adding the condition of password_verify is incorrect.

You can use the below code. I have tested it.

<?php
$password = [
    'john'   => 'password1',
    'paul'   => 'password2',
    'george' => 'password3',
    'robert'  => 'password4'
];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $pass = @$password [ $_POST['username'] ];
    if (!isset($pass))
    {
        echo "Invalid log in<br>";
    }
    else
    {
        if ($_POST['password'] == $pass)
        {
            echo "Log in successful<br>";
        }
        else
        {
            echo "Invalid log in<br>";
        }
    }
}
?>

<body>    
    <center> <h1>Login</h1> </center>   
    <form action="" method="POST">
        <div class="container">   
            <label>Username:</label>   
            <input type="text" placeholder="Enter Username" name="username" required>  
            <label>Password:</label>   
            <input type="password" placeholder="Enter Password" name="password" required>  
            <button type="submit">Login</button>   
            <input type="checkbox" checked="checked"> Remember me   
            <button type="button" class="cancelbtn"> Cancel</button>   
            <a href="">Forgot password?</a>
        </div>   
    </form>
</body>     
</html>

Upvotes: 1

Related Questions