Jump to content

login form using array


perezf

Recommended Posts

i am trying to make this login script using arrays to work but my login always comes back incorrect
what could be the problem
[code]//assigns the username and password
$username = array("admin", "frank", "money");
$password = array("password", "thepass", "test");

//reads the users input and assigns a name
$user = $_POST['user'];
$pass = $_POST['pass'];


//checks to see if the username and password are correct
if ($user == $username[count($username)] && $pass == $password[count($password)]) {[/code]
Link to comment
https://forums.phpfreaks.com/topic/20304-login-form-using-array/
Share on other sites

instead of

if ($user == $username[count($username)] && $pass == $password[count($password)]) {

try

if ($user == $username[(count($username) - 1)] && $pass == $password[(count($password) - 1)]) {


but I'm curious how this authentication works? you can only check if the user is the last user or it will always do the wrong thing.

If you intend to define passowords and usernames in an array, which I strongly recommend to keep them in a database, you could something like this.

$auth = array("admin" => "password", "frank" => "thepass", "test" => "money");

then in your if statement do this

if ( $auth[$user] == $pass )
echo "you logged in...";
Link to comment
https://forums.phpfreaks.com/topic/20304-login-form-using-array/#findComment-89405
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.