Jump to content

Archived

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

perezf

login form using array

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]

Share this post


Link to post
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...";

Share this post


Link to post
Share on other sites
because the array begins from 0th element and not first your count($username) will return 3 and there is no element at 3. Instead there are 0,1 and 2.

Share this post


Link to post
Share on other sites

×

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.