Jump to content


Photo

PHP Question Regarding Arrays


  • Please log in to reply
6 replies to this topic

#1 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 19 August 2006 - 12:10 AM

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:

<?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;
		
	?>

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.

#2 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 19 August 2006 - 12:19 AM

You could do it like this:

$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;
     }
}


#3 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 19 August 2006 - 12:44 AM

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

I have made this code my own, like so:

<?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;
			
}
?>

Now when I try to login with say, pete > feet. It comes up like so: not loggedinnot logged innot logged in 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.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 August 2006 - 01:45 AM

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:

<?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");	
	}
		
?>



#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 19 August 2006 - 01:40 PM

try
<?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;
?>


#6 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 19 August 2006 - 06:48 PM

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.

<?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;
?>

Notice how I am using a mysql_query() 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 The supplied username and/or password we're incorrect. 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.

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 August 2006 - 07:01 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users