首页 > 解决方案 > 函数中的 $_SESSION 变量未传递到下一页

问题描述

在我忘记密码的功能中,我正在启动会话,设置会话 ID 并尝试将电子邮件存储在 $_SESSION 变量中。然而,变量没有被存储,但会话 id 被存储。我不确定我做错了什么。我已经研究了 php 手册和几个关于此的堆栈溢出帖子,但我似乎无法使其工作。我不断收到错误消息:未定义索引:第 3 行 /home/adamhardy603/public_html/resetPassword.php 中的电子邮件

功能:

session_start();

function forgot_submit(){
        GLOBAL $db;
        if (isset($_GET['forgot']) && $_GET['forgot'] == 'true') {
            $email     = $_POST['forgot_password'];   ///////////////// The password they just entered /////////////////
            $token     = "1234567890qwertyuiopasdfghjklzxcvbnm";
            $token     = str_shuffle($token);
            $token     = substr($token, 0, 10);
            mail($email, "Password Reset", "To reset you password, please enter this code: $token", "from: noreply@yahbang.com");
            $Query = $db->prepare("UPDATE user SET forgotToken='$token' WHERE email='$email'");////email is being sent and token set in db///
            $Query->execute();
            if ($Query) {
                $sid = md5(uniqid());
                session_id($sid);
                $_SESSION['email'] = $email; //////////////////$_SESSION['email'] is not being passed to the next page///////////
                echo json_encode(['error' => 'success', 'msg' => 'resetPassword.php']);
            }

        }
    }////close forgot submit method///////////
    forgot_submit();

下一页我希望将变量传递给:resetPassword.php

<?php
session_start();
$email = $_SESSION['email'];
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 shrink-to-fit=no">
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <title>Yahbang</title>
</head>

<body>

    <?php
        echo session_id();
        Print_r ($_SESSION);
    ?>

    <div class="container">
        <div class="row">

            <!--- col --->
            <div class="col-md-4 content mx-auto">

                <div class="verify-cover">
                    <div class="card">
                        <div class="card-header">
                            <div class="row">
                                <div class="col-md-9">
                                    <h3 class="form-heading">Sorry you have to reset your password</h3>
                                    <p>Please type the code we sent to your email</p>
                                </div>

标签: php

解决方案


推荐阅读