-1
I have the following code, if I do not make a particular request it sends an error, if there is no such error send email to the user. the problem is that in addition to appearing the error and also sends the email (pf see the photo).
<?php
include("config.php");
if(isset($_POST\['submit_email'\]) && isset($_POST\['email'\])) {
$email = mysqli_real_escape_string($conn, $_POST\['email'\]);
$sql = "SELECT * FROM registo WHERE email = '$email'";
$r = mysqli_query($conn, $sql);
$count = mysqli_num_rows($r);
if($count == 1) {
// Create new hash
$key = hash('sha256', uniqid("", true));
// SQL query to update user record with hash value
$usql = "UPDATE registo set reset_key = '".$key."' where email = '".$email."'";
if(!mysqli_query($conn, $usql)) {
echo "Error updating database!";
}
// send link to user with generated key
$link="<a href='http://unn-w17015779.newnumyspace.co.uk/reset.php?key=".$key."'>Click To Reset password</a>";
$to = $email;
$subject = 'Reset Password';
$message = 'Click On This Link to Reset Password '.$link;
$headers = 'From: Galaxy books shop <**@gmail.com>' . "\r\n" .
'Reply-To: **@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send email
if(mail($to, $subject, $message, $headers)){
echo "Your reset link has been sent to your email ";
}else{
echo "Failed to Recover your password, try again";
}
} else {
echo "User name does not exist";
}
}
?>
The sending of the email is outside the if that checks the error in the update, this is not a problem of PHP or SQL but rather a programming logic problem.
– Gustavo Jantsch
"if you do not make a request, issue an error" if you refer to that code snippet? echo "Error updating database!";
– Don't Panic
Just so you’re aware
uniqid()
It’s time-based, it’s good for generating unique data, but not for generating hard-to-find data, that’s different. It uses an extremely predictable generator LGC, Linear congruential Generator,. PHP already has support for CSPRNG, with therandom_bytes()
or if you want to trust Userspace CSPRNG has theopenssl_random_pseudo_bytes()
. There is even a RFC to improve uniqid(), currently closed.– Inkeliz