Click to See Complete Forum and Search --> : [Resolved] Hello again :-) Making a login (arrays)!!!


Johno M
05-23-2005, 04:14 PM
OK! I've just started PHP and I'm slowly getting there. However, I've hit a snag with arrays :-/

You see, I have made a login, and an area you can only get in using this pass. At the moment I can only make it so that I use 1 username and 1 password. I tried using arrays but I can't find a way to make it so that one username goes with one password.... I'll explain.

<?
// the variables for the user/pass taken from an exterior form.
$username = $_POST['user'];
$password = $_POST['pass'];


// location of page to go to if username and password match
$url = "!!!!!!!!!!!!!!!!!!ADDRESS!!!!!!!!!!!!!!";
$urlfail =
"!!!!!!!!!!!!!!!!!!ADDRESS!!!!!!!!!!!!!!";


// the arrays for user and pass
$user_array = array("user1","user2","user3","user4","user5");
$pass_array = array("pass1","pass2","pass3","pass4","pass5");
$user_length = count( $user_array );


// this is where it goes screwy, this is wrong!!!
for ( $index=0; $index < $user_length; $index++ )
{
if ( $username == $user_array[$index] && $password == $pass_array[$index] )
{

header("Location: $url");

} else {

header("Location: $urlfail");
}
}
?>

I dont know how to make the second username to work with the second password :-/ any ideas? Thank you for reading :-)

xblue
05-24-2005, 08:48 AM
Seems to be a logical problem: you redirect the first time the code in the for loop is executed instead of waiting with the redirection for all array elements to be processed.

Should be easy to fix I think, e.g. store the success of matching the login in a variable and redirect based on that after the loop.

Hope that makes some sense.

BTW, how does that relate to Windows?

Johno M
05-24-2005, 09:02 AM
Its running on a windows server, so I thought I'd post it in here
:-)

Ok, i'm gunna risk sounding like a dumbass but I don't quite get what you mean.... :-D

xblue
05-24-2005, 09:46 AM
I don't do this too often, but maybe the code helps you to follow what I was trying to say.


$valid = FALSE;
for ( $index=0; $index < $user_length; $index++ ) {
if ( $username == $user_array[$index] && $password == $pass_array[$index] ) {
$valid = TRUE;
} // no else-block needed
}
if ($valid) {
header("Location: $url");
} else {
header("Location: $urlfail");
}

Note: this is not tested, it still may have typos left for you to fix.

If you are not sure it is windows specific, I'd recommend choosing the newbies forum for now, folks tend to be even more patient around there ;)

Johno M
05-24-2005, 10:41 AM
yey! Its working! your my god :-D

I did think about posting in the newbies section but I'v been having so many problems with my windows webspace I thought it would be best placed in here. Haha, nevermind

Thank you so much! :-)