I want to create multiple users to enter a site (without a database).
Let's say I've generated a user and password combination in PHP with password_hash. (Method found here.)
$userhash = password_hash('exampleusername', PASSWORD_BCRYPT, $useroptions);
$passhash = password_hash('examplepass', PASSWORD_BCRYPT, $pwoptions);
Then I saved the hashed files somewhere.
$hasheduser = file_get_contents("private/user");
$hashedpass = file_get_contents("private/pass");
Set the username and password combination against the hash for a $_POST from a form.
$userhash = password_hash($_POST['user'], PASSWORD_BCRYPT, $useroptions);
$passhash = password_hash($_POST['pass'], PASSWORD_BCRYPT, $pwoptions);
Now if the user/pass combo are correct, a session will be set and checked.
if (
(password_verify($_POST['user'], $hasheduser))
&& (password_verify($_POST['pass'], $hashedpass))
) {
session_start();
$_SESSION['validuser'] = $_POST['user'];
}
My question is, how can I create multiple usernames and passwords without constantly duplicating the same code?
Making multiple users...
$hasheduser = file_get_contents("private/user");
$hashedpass = file_get_contents("private/pass");
$hasheduser2 = file_get_contents("private/user2");
$hashedpass2 = file_get_contents("private/pass2");
Checking multiple users...
if (
(password_verify($_POST['user'], $hasheduser))
&& (password_verify($_POST['pass'], $hashedpass))
) elseif (
(password_verify($_POST['user'], $hasheduser2))
&& (password_verify($_POST['pass'], $hashedpass2))
)
Is there a way to loop through users to enter the login screen instead of multiple elseif statements?
Any help appreciated.
I'm thinking maybe I need to put the user/pass combos in an array...(my incorrect attempt).
$users = array(
file_get_contents("private/user1") => file_get_contents("private/pass1"),
file_get_contents("private/user2") => file_get_contents("private/pass2")
);
foreach ($users as $key => $value) {
if ((password_verify($username, $key)) && (password_verify($pass,$value))) {}
}
But that would just endlessly duplicate the login form.
Thank you, any help is appreciated.