代码之家  ›  专栏  ›  技术社区  ›  Sean Mitchell

PHP$_会话在函数中不工作

  •  1
  • Sean Mitchell  · 技术社区  · 8 年前

    首先,我搜索了很多线程和主题,他们都在说它的函数位置,但到目前为止,我没有看到我的位置有问题。我非常想让它工作,因为我厌倦了看50多行重复代码。

    重置密码。php(相关代码):

    <?php
            require_once $_SERVER['DOCUMENT_ROOT'] . '/php/security/sslcheck.php';
    
            $ResetID = $_GET["ID"];
    
            session_start();
            require_once $_SERVER['DOCUMENT_ROOT'] . '/php/functionality/error.php';
            print_r(array_values($_SESSION));
    
            if(empty($ResetID) && !isset($_SESSION["ERR"]) && !isset($_SESSION["ERR_MSG"])) {
                $ResetPassword = "REQUEST";
                if(isset($_POST["email-search"]) && !empty($_POST["email-search"])) {
                    require_once $_SERVER['DOCUMENT_ROOT'] . '/php/functionality/users/request.php';
    
                    // Start Request Process
                    $Request = RequestPasswordReset($_POST["email-search"]);
    
                    if($Request === "USER_NOT_FOUND") {
                        DisplayError("Password Reset Request Failed", "The specified email is not tied to any accounts in our system.", "/account/resetpassword.php");
                    } else if ($Request === "MAIL_FAILURE") {
                        DisplayError("Password Reset Request Failed", "Failed to email you the password reset email.", "/account/resetpassword.php");
                    } else {
                        DisplayError("Password Reset Request Success", "We have sent a email that contains a link to reset your password.", "/account/resetpassword.php");
                    }
                }
    

                <?php
                if (isset($_SESSION["ERR"]) && isset($_SESSION["ERR_MSG"])) {
                    echo '<div id="resetPasswordStatus">';
                    echo '<h4>' . $_SESSION["ERR"] . '</h4>';
                    echo '<p>' . $_SESSION["ERR_MSG"] . '</p>';
                    echo '</div>';
                    session_destroy();
                }
            ?>
    

    错误php(所有代码):

    <?php
      session_start();
      function DisplayError($title, $body, $returnlink) {
          $_SESSION["ERR"] = $title;
          $_SESSION["ERR_MSG"] = $body;
          header("Location: " . $returnlink);
      }
    ?>
    

    预期输出是在回调后请求密码重置,是要显示的警报。此警报的代码可在更相关的代码下查看。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Sean Mitchell    8 年前

    解决方案:

    错误php 如果您添加 exit(); 在头重定向之后,它允许PHP在重定向发生时暂停执行,从而防止错误显示代码运行其代码,然后擦除会话,从而使重定向在会话方面为空。

        2
  •  0
  •   mega6382    8 年前

    empty($ResetID) 如果 $_GET['ID'] 不是空的,因此将其更改为 !empty($ResetID)