Jump to content

PHP Question Regarding Arrays


CMellor

Recommended Posts

Hello,

I have come here to hopefully seek an answer to a question I have about PHP arrays. First off, I have been an on and off PHP/MySQL developer for 5 years now, but have always worked on my own projects. One thing I never got into was arrays, at first they looked a bit scary and it puts me off wanting to learn it (sounds wussy, I know) I also have books with chapters on arrays, and they do make a bit more sense to me now, but I cannot find a use for them.

Currently I am working on a small login script, I don't want to go too overboard because this isn't a big project, so I am doing it the easy way, which is this:

[code]<?php else: // if login is pressed

$username = $_POST['username'];
$password = $_POST['password'];

if($username == "chris" && $password == "meh"):

echo("logged in");

else:

echo("not logged in");

endif;

?>[/code]

I am guessing that you could do this using arrays right, but I wouldn't know where to begin. I am hoping that I can get some advice on how to use arrays to store usernames and passwords and use them when checking for a login. I am not even sure if this can be done, but I bet it can.

I am asking for this help to expand my knowledge on my favourite language to use. I have checked countless tutorial websites but I never see what I am seeking, so I can't wait to here you're answers (if I get any)

Thanks for your time.

Chris.
Link to comment
Share on other sites

You could do it like this:

[code=php:0]
$usernames = array("someusername", "anotherusername", "another_username");
$passwords = array("firstpassword", "secondpassword", "thirdpassword");

// Then get those variables like this:
// $usernames[0] will be "someusername" and $password[0] will be that persons password
// $usernames[1] will be "anotherusername" and $password[1] will be that persons password...etc.

// Go through a loop for all the usernames and passwords and check each one like this:
// count() will get the number of variables in an array

for ($x=0; $x < count($usernames); $x++) {
    if ($username == $usernames[$x] && $password = $passwords[$x]) {
          $loggedin = true;
    }
}
[/code]
Link to comment
Share on other sites

Nice bit of code their and yes it does work... kind of.

I have made this code my own, like so:

[code]<?php
$usernames = array("chris", "bob", "pete");
$passwords = array("piss", "knob", "feet");

for($x = 0; $x < count($usernames); $x++) {
if(@$username == $usernames[$x] && $password == $passwords[$x]):

echo("logged in");

else:

echo("not logged in");

endif;

}
?>[/code]

Now when I try to login with say, pete > feet. It comes up like so: [i]not loggedinnot logged innot logged in[/i] which means it didn't work, and it also says that when I put the wrong details in. Do you have any advice on this?

Thanks for taking the time to help me out, I really appreciate it.
Link to comment
Share on other sites

That is a bad way of doing it...not only is it inefficient having to loop through the array, but what happens when you have 10 users and you accidentally get a password in the wrong place...then every user after that one will not be able to login.

Rather then two arrays, use key => value pairs in a single array:

[code]<?php
$username = $_POST['username'];
$password = $_POST['password'];


$authusers = array(
'chris' => 'piss',
'bob' => 'knob',
'pete' => 'feet'
};

if ($password == $authusers[$username]) {
echo("logged in");
} else {
echo("not logged in");
}

?>[/code]

Link to comment
Share on other sites

try[code]
<?php
$usernames = array("chris", "bob", "pete");
$passwords = array("piss", "knob", "feet");
$username = $_POST['username'];
$password = $_POST['password'];
$log_in = 'not logged in';
for($x = 0; $x < count($usernames); $x++) {
if(@$username == $usernames[$x] && $password == $passwords[$x]):
$log_in = 'loged in';//echo("logged in");
//else:
//echo("not logged in");
endif;
}

echo $log_in;
?>
[/code]
Link to comment
Share on other sites

Thank you guys for your code example's, they are really helpful! Could I direct you to this code which is modified to what sasa provided for me.

[code]<?php
$usernames = array("chris", "bob", "pete");
$passwords = array("piss", "knob", "feet");
$login = "The supplied username and/or password we're incorrect.";

for($x = 0; $x < count($usernames); $x++) {
if(@$username == $usernames[$x] && $password == $passwords[$x]):

$query = mysql_query("SELECT id, name FROM $dbname.app_roster ORDER BY name ASC") or die(mysql_error());
while($row = mysql_fetch_array($query)) {

$login =  $row['name'].'<br />';

}

endif;
}

echo $login;
?>[/code]

Notice how I am using a [b]mysql_query()[/b] to grab some data from the datasbase? Here is my situation:

When correctly logged in, the big list of name's will show up, but underneath the list of name's, it will also say [i]The supplied username and/or password we're incorrect.[/i] which is the error message if a login is incorrect, yet still displays underneath the SQL results. I've tried all sort's of way's to try and solve this but have got nowhere.

Hopefully somebody can solve my problem and I hope I am not becoming too much of a fuss, I just want to learn and this is a great place to do it!

Thank you again for taking the time to help.

Chris.
Link to comment
Share on other sites

Hitman explained why jeremysr's code was poor and gave you a better solution. A sasa's is mostly the same as jeremysr's I wonder why you went with that one. ???

Also I wonder what you are trying to achieve with your query. As it reads every record it always sets login to last name in table.
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.