Reputation: 625
I have the following code:
if(strcmp($_POST['password'], $_POST['password-rpt']) == 0) {
$password_field = $_POST['password'];
echo "Password created" . "<br />";
} else {
echo "blarg! Password mismatch!!!";
}
I know that like C/C++ strcmp is supposed to return 0 when the two strings are the same but when I test it with an html file, entering two identical strings or two different ones, both cases go to the error case and my output is "blarg! Password mismatch!!!"
Here is the form I used:
<form method="post" action="register.php">
Name: <input type="text" name = "name" id="name" /><br />
Password: <input type="password" name ="password" id = "password" /> <br />
Confirm Password: <input type="password" name="password_rpt" id="password_rpt" /> <br />
email: <input type="text" name = "remail" id = "remail" /><br />
<input type = "submit" name="register" id="register" value = "Sign Up" />
</form>
What am I missing?
Update:
I changed the condition to if($_POST['password'] == $_POST['password-rpt'])
. Hasn't helped.
Upvotes: 2
Views: 3408
Reputation: 63580
You can simply use:
$_POST['password'] === $_POST['password-rpt']
Also, in your form, you used the name password_rpt
, while in your code, you refer to password-rpt
. You need to change one of them so that the names match.
Take a look at the Strings manual page to learn more about string handling in PHP. I also recommend having a look at the Language Reference for general guidance.
Do keep in mind that PHP is a high level scripting language, and is very different from C++, even if they might look the same. There are things that are very feasible in C++ but not in PHP, and vice versa.
Upvotes: 4
Reputation: 5613
Keep in mind that strcmp()
returns:
str1
is less than str2
; str1
is greater than str2
;they
are equal;Similar to ==
(equal operator) as in strcmp('1e3', '1000')
(return value 0 ), or '1e3'=='1000'
(true).
Note, PHP 7 have a similar operator, spaceship operator (<=>) and have the same behaviour than strcmp()
.
In your case you should use :
if ($_POST['password'] === $_POST['password-rpt'])
Upvotes: 0
Reputation: 4213
For passwords you really want to be using === (identical) as == (equal) will return true even if case does not match...
if( $_POST['password'] === $_POST['password-rpt'] ) {
$password_field = $_POST['password'];
echo "Password created" . "<br />";
} else {
echo "blarg! Password mismatch!!!";
}
Upvotes: 2
Reputation: 1959
Why are you using strcmp()
? You'd get the desired result if you did
if ($_POST['password'] == $_POST['password-rpt'])
Upvotes: -1