Jump to content


Photo

login form using array


  • Please log in to reply
4 replies to this topic

#1 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 04:05 PM

i am trying to make this login script using arrays to work but my login always comes back incorrect
what could be the problem
//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)]) {


#2 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 10 September 2006 - 04:12 PM

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...";
Roy Simkes
Yet Another Parkyeri Developer

#3 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 04:15 PM

why do you use -1

#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 10 September 2006 - 04:18 PM

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.
Roy Simkes
Yet Another Parkyeri Developer

#5 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 04:31 PM

ok got it thank you




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users