英文:
I need to Display the email of the user who made the forget password request
问题
我需要显示请求重置密码的用户的电子邮件地址
当用户在forgetPassword.php中输入他的电子邮件地址时,如果电子邮件地址存储在数据库中,将从forgetPasswordProcess.php发送OTP代码到该电子邮件地址。否则,不会发送代码,并且用户输入OTP的页面将不会出现。
forgetPasswordProcess.php
<?php
$server_name = "localhost";
$user_name = "root";
$dbpassword = "";
$databaseName = "database_2";
$forgetPwEmail = $_POST['forgetPwEmail'];
$connection = new mysqli($server_name, $user_name, $dbpassword, $databaseName);
$query = "SELECT * FROM database_2.user WHERE `email`= '$forgetPwEmail' OR `phone` = '$forgetPwEmail';";
if ($connection->connect_error) {
die("connection failed: " . $connection->connect_error);
}
$result = $connection->query($query);
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;
if (isset($_POST["send"])) {
$code = rand(100000, 600000);
$mail = new PHPMailer(true);
try {
if ($result->num_rows > 0) {
// 电子邮件/电话在数据库中找到
$row = $result->fetch_assoc();
$email = $row['email'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'kaveeshachamod3@gmail.com';
$mail->Password = 'gjzmvmyqeottyvjs';
$mail->SMTPSecure = 'ssl';
$mail->Port = '465';
$mail->setFrom('kaveeshachamod3@gmail.com');
$mail->addAddress($_POST["forgetPwEmail"]);
$mail->isHTML(true);
$mail->Subject = "OTP CODE";
$mail->Body = "Your OTP CODE is: " . $code;
if ($forgetPwEmail === $email) {
if ($mail->send()) {
// 邮件发送成功
// 将代码存储在数据库中以进行验证
$sql = "UPDATE database_2.user SET OTP_code = '$code' WHERE `email` = '$forgetPwEmail' OR `phone` = '$forgetPwEmail'";
if ($connection->query($sql) === true) {
header("Location: OTPCode.php");
exit;
} else {
echo 'Error: ' . $sql . '<br>' . $connection->error;
}
} else {
// 邮件发送失败
header("Location: forgetPassword.php");
exit;
}
} else {
echo header('Location: forgetPasswordProcess.php');
}
} else {
echo "Email not found in the database";
}
} catch (Exception $e) {
// 异常发生
echo 'Error sending email: ' . $e->getMessage();
}
}
?>
OTPCode.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enter Your OTP code</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="main-form">
<form action="OTPprocess.php" method="post">
<div class="form-details">
<h3>Enter Your OTP</h3>
<input type="text" name="otp" placeholder="Enter Your OTP Here">
</div>
<button type="submit" class="submit-btn">Submit</button>
<h5>OTP send successfully to <?php echo $forgetPwEmail ?>. please check your email</h5>
</form>
</div>
</div>
</body>
</html>
在上述代码中,我将用户的电子邮件地址存储在了$forgetPwEmail
变量中,并在OTPCode.php中使用<?php echo $forgetPwEmail ?>
来显示该电子邮件地址。
英文:
I need to Display the email of the user who made the forget password request
When the user enters his email in forgetPassword.php, the OTP code is sent to the email from forgetPasswordProcess.php (if the email is stored in the database). Otherwise, the code will not be sent, and the page where the user will type the OTP will not appear.
forgetPasswordProcess.php
<?php
$server_name = "localhost";
$user_name = "root";
$dbpassword = "";
$databaseName = "database_2";
$forgetPwEmail = $_POST['forgetPwEmail'];
$connection = new mysqli($server_name , $user_name , $dbpassword , $databaseName);
$query = "SELECT * FROM database_2.user WHERE `email`= '".$forgetPwEmail."' OR `phone` = '".$forgetPwEmail."';";
if($connection->connect_error){
die("connection failed: ". $connection->connect_error);
}
$result = $connection->query($query);
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;
if(isset($_POST["send"])) {
$code = rand(100000,600000);
$mail = new PHPMailer(true);
try {
if ($result->num_rows > 0) {
// Email/Phone found in the database
$row = $result->fetch_assoc();
$email = $row['email'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'kaveeshachamod3@gmail.com';
$mail->Password = 'gjzmvmyqeottyvjs';
$mail->SMTPSecure = 'ssl';
$mail->Port = '465';
$mail->setFrom('kaveeshachamod3@gmail.com');
$mail->addAddress($_POST["forgetPwEmail"]);
$mail->isHTML(true);
$mail->Subject = "OTP CODE";
$mail->Body = "Your OTP CODE is: ". $code;
if ($forgetPwEmail === $email) {
if ($mail->send()) {
// Email sent successfully
// Store the code in the database for verification
$sql = "UPDATE database_2.user SET OTP_code = '$code' WHERE `email` = '$forgetPwEmail' OR `phone` = '$forgetPwEmail'";
if ($connection->query($sql) === true) {
header("Location: OTPCode.php");
exit;
} else {
echo 'Error: ' . $sql . '<br>' . $connection->error;
}
} else {
// Failed to send email
header("Location: forgetPassword.php");
exit;
}
}else {
echo header('Location: forgetPasswordProcess.php');
}
}else {
echo "Email not found in the database";
}
} catch (Exception $e) {
// Exception occurred
echo 'Error sending email: ' . $e->getMessage();
}
}
?>
OTPCode.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enter Your OTP code</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="main-form">
<form action="OTPprocess.php" method="post">
<div class="form-details">
<h3>Enter Your OTP</h3>
<input type="text" name="otp" placeholder="Enter Your OTP Here">
</div>
<button type="submit" class="submit-btn">Submit</button>
<h5>OTP send successfully to *"user email"*. please check your email</h5>
</form>
</div>
</div>
</body>
</html>
I want to show the email of the user who requested the forget password in the place I have shown above
I tried this way. But I god error called "undefined assigned Variable".
forgetPasswordProcess.php
<?php
session_start();
// Your existing code...
if ($result->num_rows > 0) {
// Email/Phone found in the database
$row = $result->fetch_assoc();
$email = $row['email'];
if ($forgetPwEmail === $email) {
if ($mail->send()) {
// Email sent successfully
// Store the code in the database for verification
$sql = "UPDATE database_2.user SET OTP_code = '$code' WHERE `email` = '$forgetPwEmail' OR `phone` = '$forgetPwEmail'";
if ($connection->query($sql) === true) {
$_SESSION['email'] = $email; // Store the email in a session variable
header("Location: OTPCode.php");
exit;
} else {
echo 'Error: ' . $sql . '<br>' . $connection->error;
}
} else {
// Failed to send email
header("Location: forgetPassword.php");
exit;
}
} else {
echo header('Location: forgetPasswordProcess.php');
}
} else {
echo "Email not found in the database";
}
// Your existing code...
?>
OTPCode.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enter Your OTP code</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="main-form">
<form action="OTPprocess.php" method="post">
<div class="form-details">
<h3>Enter Your OTP</h3>
<input type="text" name="otp" placeholder="Enter Your OTP Here">
</div>
<button type="submit" class="submit-btn">Submit</button>
<h5>OTP send successfully to <?php echo $forgetPwEmail ?>. please check your email</h5>
</form>
</div>
</div>
</body>
</html>
答案1
得分: 1
你应该使用会话来在请求之间保存服务器状态。
只需将电子邮件保存到 $_SESSION 中:$_SESSION['otp_email'] = $row['email'];
,然后以以下方式访问它:<h5>OTP 发送成功至 <?php echo $_SESSION['otp_email'] ?? '' ?></h5>。请检查您的电子邮件。
成功检查 OTP 后,请不要忘记清除 $_SESSION['otp_email']
变量。
我还建议在整行上添加 IF 语句,以防没有存储电子邮件时不显示它,这样它就不会变成 <h5>OTP 发送成功至 . 请检查您的电子邮件</h5>
。
我不建议使用 $_GET
来传递状态,因为这可能导致虚假的状态(例如,如果有人直接使用地址)。
英文:
You should use sessions to save server state between requests.
Just save the email into the $_SESSION: $_SESSION['otp_email'] = $row['email'];
and then access it the way like <h5>OTP send successfully to <?php echo $_SESSION['otp_email'] ?? '' ?>. please check your email</h5>
. After OTP successfully checked, don't forget to clear the $_SESSION['otp_email']
variable.
I also recommend adding IF statement on the entire line to not show it if there is no email stored, so it won't become <h5>OTP send successfully to . please check your email</h5>
.
I can't recommend using $_GET
to transfer the state because it may lead to fake status (like if somebody uses the address directly).
答案2
得分: 0
使用header();
进行重定向时,基本上会丢失所有先前定义的变量。
因此,既然你将其存储在会话变量中,你可以像这样访问它:<h5>已成功发送OTP至 <?= $_SESSION['email'] ?? '' ?>。请检查你的电子邮件</h5>
。
但是,如果是我,我会通过GET方式发送电子邮件,如此重定向:header("Location: OTPCode.php?email={$row['email']}");
,然后在模板中可以像这样访问它:<h5>已成功发送OTP至 <?= $GET['email'] ?? '' ?>。请检查你的电子邮件</h5>
。
英文:
When you redirect with header();
, you basically lose all the previously defined variables.
So, since you stored it in a session variable, you can access it like this <h5>OTP send successfully to <?= $_SESSION['email'] ?? '' ?>. please check your email</h5>
.
But, if it were me, I would send the email via GET header("Location: OTPCode.php?email={$row['email']}");
, and you could access it it the template something like this <h5>OTP send successfully to <?= $GET['email'] ?? '' ?>. please check your email</h5>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论