newbie question about associative arrays/functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
filyr
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 7:04 pm

newbie question about associative arrays/functions

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: newbie question about associative arrays/functions

Post by requinix »

Where does the code check the password? All it does now is call a function. Itself, actually.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: newbie question about associative arrays/functions

Post 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.
filyr
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 7:04 pm

Re: newbie question about associative arrays/functions

Post 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 :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: newbie question about associative arrays/functions

Post 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.
filyr
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 7:04 pm

Re: newbie question about associative arrays/functions

Post 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?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: newbie question about associative arrays/functions

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: newbie question about associative arrays/functions

Post 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
}
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie question about associative arrays/functions

Post by jackpf »

I prefer

Code: Select all

if(!sizeof($_POST))
//not submitted
;



Or maybe

Code: Select all

if($_SERVER['REQUEST_METHOD'] != 'POST')
//not submitted
;
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie question about associative arrays/functions

Post by jackpf »

Wow, my post got messed up 8O


Stupid phpBB.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: newbie question about associative arrays/functions

Post by Ollie Saunders »

jackpf wrote:I prefer

Code: Select all

if(!sizeof($_POST))
//not submitted
;
Interesting. Why do you prefer that?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie question about associative arrays/functions

Post 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 :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: newbie question about associative arrays/functions

Post 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.
Last edited by Ollie Saunders on Fri Sep 25, 2009 8:00 am, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie question about associative arrays/functions

Post by jackpf »

8O

Yeah. Kind of stopped understanding the last few paragraphs....but yeah :P

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'].
Post Reply