Page 1 of 1
newbie question about associative arrays/functions
Posted: Wed Sep 23, 2009 7:10 pm
by filyr
hello, im new to both php and this forum

started with php yesterday btw.
Code: Select all
function check($name,$password,$users){
$users = array("leonard" => "skalbagge",
"floyd" => "fraktal",
"morisson" => "42");
if(check("leonard","skalbagge",$users))
{
print "match";
}
}
check("leonard","skalbagge",$users);
Im trying to make a password-checker. its going to compare $name and $password with the associative array and print match whenever theres a match. Why doesnt this work?
Re: newbie question about associative arrays/functions
Posted: Wed Sep 23, 2009 7:37 pm
by requinix
Where does the code check the password? All it does now is call a function. Itself, actually.
Re: newbie question about associative arrays/functions
Posted: Wed Sep 23, 2009 11:57 pm
by Ollie Saunders
Yeah, there's no actual testing logic in your function. Also recursives are quite an advanced programming technique so you might want to avoid functions that call themselves unless you need them.
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 4:48 am
by filyr
The point is to make a form with 2 input boxes, $name and $password, and check if it matches with the $users array when you push the "log in" button or something similar. I know how to make the forms and get the values etc, but i dont know how to compare it with the array as i mentioned. so any help here is appreciated

Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 6:08 am
by Ollie Saunders
You'll find the values you need from the form in the arrays $_GET or $_POST. Google about PHP arrays.
You can make the equality comparison with the == or === operators; I recommend the latter.
var_dump() can be used to show the contents of a variable, which will tell you what's going on.
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 8:27 am
by filyr
ok so i remade the code:
Code: Select all
if (isset($_POST['input'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == "hello" && $password == "123") {
print ("gratz");
}
else {
print ("wrong user/pw");
}
}
?>
<body>
<form action="apa.php" method="POST">
Username<input type="text" value="" size="15" name="username" /> <br />
Password<input type="password" value="" size="15" name="password" /> <br />
<input type="submit" value="Logga in" name="input" />
</form>
but how do i use my old $users array to check if the username + password matches with that? for example if $username=="leonard" and $password=="skalbagge", witch matches with the $user array, the user should get some kind of message back.
edit:
Code: Select all
if ($username == "name" &&[b] $users[$username] == $password[/b])
i guess this part is enough?
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 10:17 am
by Mirge
Code: Select all
<?php
$users = array(
"leonard" => "skalbagge",
"floyd" => "fraktal",
"morisson" => "42");
$username = $_POST['username']; // user submitted floyd
$password = $_POST['password']; // user submitted fraktal
if(isset($users[$username]) {
if($password == $users[$username]) {
// valid login
} else {
// correct username, bad password
}
} else {
// incorrect username
}
?>
I'd do something like that.
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 2:47 pm
by Ollie Saunders
Try:
Code: Select all
if (isset($users[$username]) && $users[$username] === $password)) { ...
... or, if it makes more sense to you:
Code: Select all
if (array_key_exists($username, $users) && $users[$username] === $password)) { ...
Also, if you want to tell if the form was submitted, I recommend:
Code: Select all
if (!empty($_POST)) {
// submitted
} else {
// not submitted
}
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 2:52 pm
by jackpf
I prefer
Code: Select all
if(!sizeof($_POST))
//not submitted
;
Or maybe
Code: Select all
if($_SERVER['REQUEST_METHOD'] != 'POST')
//not submitted
;
Re: newbie question about associative arrays/functions
Posted: Thu Sep 24, 2009 2:53 pm
by jackpf
Wow, my post got messed up
Stupid phpBB.
Re: newbie question about associative arrays/functions
Posted: Fri Sep 25, 2009 4:19 am
by Ollie Saunders
jackpf wrote:I prefer
Code: Select all
if(!sizeof($_POST))
//not submitted
;
Interesting. Why do you prefer that?
Re: newbie question about associative arrays/functions
Posted: Fri Sep 25, 2009 4:27 am
by jackpf
Well, if someone submitted a blank field, or a field containing "0", then empty() would regard it as empty. sizeof() or count() would still recognise it as a populated array.
So empty() would tell you the form hasn't been submitted, when in some cases it actually has.
I actually prefer
Code: Select all
if($_SERVER['REQUEST_METHOD'] == 'POST');
Because if the request method is post, then they're at least
trying to submit something, which your script should acknowledge imo.
I also prefer dealing with an unsubmitted form before a submitted form. It just seems more logical, doing it in chronological order.
So, in your example:
Code: Select all
if(!empty($_POST))
//process form
else
//display form
I'd do it like:
Code: Select all
if(empty($_POST))
//display form
else
//then do processing
But obviously this is all just personal preference

Re: newbie question about associative arrays/functions
Posted: Fri Sep 25, 2009 6:17 am
by Ollie Saunders
Well, if someone submitted a blank field, or a field containing "0", then empty() would regard it as empty.
Code: Select all
$a = array('foo' => 0);
var_dump(empty($a)); # => bool(false)
$a = array('foo' => '');
var_dump(empty($a)); # => bool(false)
$a = array('' => '');
var_dump(empty($a)); # => bool(false)
$a = array('');
var_dump(empty($a)); # => bool(false)
$a = array();
var_dump(empty($a)); # => bool(true)
I actually prefer
Code: Select all
if($_SERVER['REQUEST_METHOD'] == 'POST');
Because if the request method is post, then they're at least
trying to submit something, which your script should acknowledge imo.
The particulars of that argument might have merit. It's not something I've thought about before. At this time I don't have the motivation to go delving into the HTTP spec to find out which way is the most correct, reliable, and true to design. But, $_SERVER['REQUEST_METHOD'] == 'POST' certainly reads better than !empty($_POST), so I think I'll probably use that from now on.
But obviously this is all just personal preference

I am contemptuousness of leaving any decision to opinion where there is a reasonable alternative. People are idiots; they will aggressively believe nonsense if left to their own devices. What we are trying to achieve is that which is simple to understand and work with; these can be assessed relatively objectively, with a good approach.
Ego (the self and self-preservation) and familiarity (the patterning system of the mind) are the forces of opinion. If these can be removed then objectivity will emerge. Ego and familiarity may be allowed to return to settle on that which has been objectively discovered afterwards; their suspension is only required during times of discovery and assessment.
It is often obvious when another's ego is affecting a decision; affecting in favor produces a smile; affecting in opposition produces a frown. I try to cultivate any awareness of these occurring in myself and others, and adjust for them appropriately.
Putting familiarity aside is more challenging. Being familiar with nothing is akin to being comatose. You can, however, approximate familiarity with nothing by being all-knowing; omniscient. Fortunately you don't actually need to know
everything, merely, as much as can be reasonably attained, about, precisely, the task at hand.
Re: newbie question about associative arrays/functions
Posted: Fri Sep 25, 2009 7:35 am
by jackpf
Yeah. Kind of stopped understanding the last few paragraphs....but yeah
Wow, they must have changed the way empty() works. I thought it would return true for an empty array, an empty string, a null string, 0, or a string of 0.
I'm at college atm, but I'll check it out tonight.
But yeah, I still prefer checking $_SERVER['REQUEST_METHOD'].