我需要显示发出忘记密码请求的用户的电子邮件

I need to Display the email of the user who made the forget password request

提问人:Kaveesha Chamod 提问时间:5/29/2023 更新时间:5/29/2023 访问量:37

问:

我需要显示发出忘记密码请求的用户的电子邮件

当用户在 forgetPassword.php 中输入他的电子邮件时,OTP 代码将从 forgetPasswordProcess.php 发送到电子邮件(如果电子邮件存储在数据库中)。否则,将不会发送代码,并且不会显示用户键入 OTP 的页面。

忘记密码进程.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 = '[email protected]';
            $mail->Password = 'gjzmvmyqeottyvjs';
            $mail->SMTPSecure = 'ssl';
            $mail->Port = '465';
    
            $mail->setFrom('[email protected]');
    
            $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();
    }
    
}

?>

OTP代码.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>

我想在上面显示的位置显示请求忘记密码的用户的电子邮件

我试过这种方式。但是我犯了一个错误,叫做“未定义的赋值变量”。 忘记密码进程.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...
?>

OTP代码.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>
php mysql 表单 变量 heidisql

评论

0赞 Community 5/29/2023
请修剪您的代码,以便更轻松地找到您的问题。请遵循这些准则,以创建最小的可重现示例
0赞 Dharman 5/29/2023
警告:你对 SQL 注入持开放态度,应该使用参数化的预准备语句,而不是手动构建查询。它们由 PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,您仍然有损坏数据的风险逃跑是不够的!

答:

0赞 Dexmente 5/29/2023 #1

当您使用 时,您基本上会丢失所有先前定义的变量。 因此,由于您将其存储在会话变量中,因此您可以像这样访问它。header();<h5>OTP send successfully to <?= $_SESSION['email'] ?? '' ?>. please check your email</h5>

但是,如果是我,我会通过 GET 发送电子邮件,您可以通过这样的模板访问它。header("Location: OTPCode.php?email={$row['email']}");<h5>OTP send successfully to <?= $GET['email'] ?? '' ?>. please check your email</h5>

1赞 woodfox 5/29/2023 #2

应使用会话来保存请求之间的服务器状态。

只需将电子邮件保存到 $_SESSION: 中,然后以如下方式访问它。OTP 检查成功后,不要忘记清除变量。$_SESSION['otp_email'] = $row['email'];<h5>OTP send successfully to <?php echo $_SESSION['otp_email'] ?? '' ?>. please check your email</h5>$_SESSION['otp_email']

我还建议在整行添加IF语句,以便在没有存储电子邮件时不显示它,因此它不会变成.<h5>OTP send successfully to . please check your email</h5>

我不建议使用来转移状态,因为它可能会导致虚假状态(例如,如果有人直接使用该地址)。$_GET