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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.