Hi,
I'm pretty new to php, having mostly worked with C#, Objective-C and Python.
I am trying to write a basic script with 1 input box and 2 buttons. The first button I want to submit a password, and the hash is then printed to the page.
My second button I want to verify the hash, and see if it matches what was submitted. If it does, I want it to print that it matches. Simple password form operation basically.
However, my second button isn't working and I'm not sure why?
$password_submitted = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$submitted_password = $_POST["password"];
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Password: <input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($submitted_password != "") {
$hash = password_hash($submitted_password, PASSWORD_DEFAULT);
echo "Password Hash: " . $hash;
}
?>
<br><br>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="submit" value="Compare Passwords">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($password_submitted == true) {
verify_password($submitted_password, $hash);
}
}
function verify_password($submitted_password, $hash) {
if (password_verify($submitted_password, $hash)) {
echo "Password match!\n";
}
}
?>
Is it because I am submitting to the same script twice? If so, how would one work around that limitation?
Kind Regards,
Morrolan