We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,231 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

In-line user database

How can I take the below array and make it an in-line array?

// Store user-data in array
$USER = array();

// first user info
$USER[1] = "username";
$USER[1] = "password";
$USER[1] = "rank";
$USER[1] = "session id";

 // second user info
$USER[2] = "username";
$USER[2] = "password";
$USER[2] = "rank";
$USER[2] = "session id";

// next user ...

When I say in-line I mean that I want to take the above two user arrays and put it in one line like the below model to save space:

$USER = array(

// First user
"username", "password", "rank", "session id"

// Second user
"username", "password", "rank", "session id"

);
4
Contributors
12
Replies
2 Days
Discussion Span
11 Months Ago
Last Updated
13
Views
Question
Answered
Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
$USER = array (
    1 => array ('username' => 'pritaeas', 'password' => 'XXX'),
    2 => array ('username' => 'Subless', 'password' => 'XXX')
);

echo $USER[1]['username'];
pritaeas
Posting Prodigy
Moderator
9,308 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,463
Skill Endorsements: 86

I have a login form and when the user clicks submit it needs to search all the $USER arrays for a match on username and password. The only problem is that my login page compares the user input like so:

if($username == $USER[???]["username"])
{
  echo 'yes';
} else
{
  echo 'no';
}

So how can I get the correct key where I have "???" before it can search the arrays for a possible match?

Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

So you want to search for a value in a multidimensional array. Slightly tricky - if it was flat you could use array_search. However, those clever people over at php manual have looked at this problem and there are a lot of solutions. Probably the easiest one I've seen is:

http://www.php.net/manual/en/function.array-search.php#92991

Alternatively, you could do a loop over the array:

$USER = array(1 => array('username'=>'diafol','password'=>'sc00byd00','rank'=>'reckless','session_id'=>'1234'), 2 => array('username'=>'ardav','password'=>'g00dis0n','rank'=>'hasbeen','session_id'=>'3456'));

$input = 'diafol';
$id = false;
foreach ($USER as $key=>$value){
    if($value['username'] == $input){
        $id = $key;
        break;
    }
}
if($id){
    echo "The id for $input is $id";    
}else{
    echo "This user doesn't exist"; 
}
diafol
Keep Smiling
Moderator
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

It works for the first entry for admin but the guest one wont work. I had to add the if else statement to check for the correct password depending on which username was submitted. It works correctly for the "admin:hello" combo, but when I use "guest:test" combo it wont work or find a match for it. It's acting as if its not going through all the $USER arrays and just going to the first one and stopping. Here's my code I have:

<?php
// User stored information
$USER = array(
  1 => array('username' => 'admin', 'password' => 'hello'),
  2 => array('username' => 'guest', 'password' => 'test')
);

// User login input
$userName = "guest";
$userPass = "test";

$id = false;

// Check user-input against user stored information
foreach($USER as $key => $value)
{
  // Check username
  if($value['username'] == $userName)
  {
    // Username was found, store key so password can be checked for that username.
    $id = $key;

    // Check password
    if($USER[$id]['password'] == $userPass)
    {
      echo 'Username and Password matches.';
      break;
    } else
    {
      echo 'Invalid username and or password.';
      break;
    }
  } else
  {
    echo 'No match found.';
    break;
  }
}
?>
Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

OK, simple use:

$USER = array(1 => array('username'=>'diafol','password'=>'sc00byd00','rank'=>'reckless','session_id'=>'1234'), 2 => array('username'=>'ardav','password'=>'g00dis0n','rank'=>'hasbeen','session_id'=>'3456'));

$msgs = array("No matches","You are logged in","Username and password do not match");


$username = 'ardav';
$password = 'g00dis0n';

$case = 0;
foreach ($USER as $key=>$value){
    if($value['username'] == $username){
        if($value['password'] == $password){
            $case = 1;          
        }else{
            $case = 2;
        }
        break;
    }
}
echo $msgs[$case];
diafol
Keep Smiling
Moderator
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

That still doesn't work. As I stated before it's acting like it only sees $USER[1] and not continuing to $USER[2]. I don't understand why its working like that. If you use the username: admin and the password: hello which is in $USER[1] it works fine, but if you enter in username: guest and password: test which is $USER[2] it will not work to save my life.

Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

ODD it works for me

diafol
Keep Smiling
Moderator
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

I don't know what's going on. I've tried foreach loops, and for loops. I just don't understand why its not noticing $USER[2]. It's only picking up on $USER[1] and stopping there.

Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I tried out both the codes - Yours and of diafol and found out that:
It's not about the loops, it's about the ' echo "messages"; and break;'
Instead of printing the message and exitting the loop, you should store the status (of finding the match) and print the message at the end (depending on the status).
Diafol's code works fine with me too.

sasankasekhar
Light Poster
35 posts since Jan 2007
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

Ah ok that works great now that I moved the messages till the end. Thanks for the tip sasankasekhar, and thanks diafol for the help with the code!

Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 11 Months Ago by diafol, pritaeas and sasankasekhar

OK, is it solved?

diafol
Keep Smiling
Moderator
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

OK, is it solved?

Yes sir Mr. diafol it's solved. I don't know why I didn't think about it, but the breaks must have been causing the loop to only run once then break out / exit.

Thank you for your great help.

Subless
Newbie Poster
11 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1305 seconds using 2.74MB