I am just working with username-to-password validation scripting, and I don’t want to mess with database connections just yet. So I’m just putting certain test emails and passwords into some arrays, then working at validating against those arrays.
I’m getting stuck on the best way to do this, because I actually want to put more pertinent data into each “user” array.
Here’s what I’ve started so far, and you’ll see what I’m shooting for:
<?php
$email = $_POST['email'];
// test emails
$logins = array('john@smith.com'=>'123456','jane@smith.com'=>'123456');
if (!array_key_exists($email, $logins)) {
echo "that email does not exist, stop here.";
}else{
echo "email exists, continue...";
}
?>
And this works fine, since it’s simple, but as I needed to add more options into the array, the method changed to this:
<?php
// tester accounts
$user1 = array('email'=>'john@smith.com','password'=>'123456','fullname'=>'john smith','handle'=>'johnny');
$user2 = array('email'=>'jane@smith.com','password'=>'123456','fullname'=>'Jane Smith','handle'=>'janeyS');
// credentials passed from a form
$email = $_POST['email'];
$pass = $_POST['pass'];
/* not quite sure how to validate the $user arrays */
?>
And the user arrays probably will grow with more things related to that user, obviously.
But I’m used to working with database connections, and not straight from PHP arrays.
Can someone throw me a little advice on a simple email/pass validation from multiple php arrays like what I just did above? Or perhaps there’s a better method?
Turn your users into a
$usersarray.This code will loop through all the users in the
$usersarray and return the subset which matched for username and password (should be either0or1).Because an empty array is falsy in PHP, casting it to Boolean should give the correct result. You could skip this if you wanted, dependent on the context of using it.