I want to make a user registration and found there some Templates and Tutorials. The registration basically works but i really want a field where the user has to repeat his password. My problem now is how to check if the two fields (password
and passwordrepeat
) are the same?
Here is my code:
Asked
Active
Viewed 67 times
-2
-
6Please do not post a picture of your code, copy it into you question. – Fabian N. Sep 23 '17 at 15:53
-
To ask an On Topic questions, please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [What topics to avoid](https://stackoverflow.com/help/dont-ask) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [take the tour](http://stackoverflow.com/tour) **We are very willing to help you fix your code, but we dont write code for you** – RiggsFolly Sep 23 '17 at 15:56
-
You're already using an API that supports **prepared statements** with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`mysqli::prepare()`](http://php.net/mysqli.prepare) and [`mysqli_stmt::bind_param()`](http://php.net/mysqli-stmt.bind-param). – Qirel Sep 23 '17 at 15:59
-
Using old methods of encrypting passwords (such as `sha1`, `md5`) are **poor methods of hashing** - you should use newer methods for hashing your passwords. PHP has a built-in [`password_hash()`](http://php.net/manual/en/function.password-hash.php) function which is a lot more secure! – Qirel Sep 23 '17 at 15:59
-
To check if two things are equal, you compare them. `$a == $b` is true if they are equal. Have a look at [**The 3 different equals**](https://stackoverflow.com/questions/2063480/the-3-different-equals) – Qirel Sep 23 '17 at 16:00
2 Answers
1
Just compare the two POST vars and check if they are equals.
if($_POST['password'] === $_POST['passwordrepeat']){
/* Do your registration */
} else {
/* return an error */
}
Obviously you have to check for the existence of those vars, and do all the security stuff.

Rafik Tighilt
- 2,071
- 1
- 15
- 27
0
Try this:
HTML Code:
<input type="password" name="pass" id="pass" min="6" max="30" required="" placeholder="Mot de passe">
<input type="password" name="cpass" id="cpass" min="6" max="30" required="" placeholder="Confirmation">
PHP Code:
<?php
if (!empty($_POST['pass']) AND !empty($_POST['cpass']) AND $_POST['pass'] == $_POST['cpass'])
{
echo "ok";
}
else
{
include("index.php");
?>
<style>
#cpass
{
border: 3px solid red;
border-radius:6px;
}
</style>
<?php
}
?>
You can also use JS.

Al Mobarmij
- 36
- 6