重定向会话变量后的 PHP 更改为 false


PHP after redirect the session variable changes to false

我创建了一个登录屏幕,用于检查密码是否正确。

提交登录表单后,我开始处理.php其中包含以下行:

if (password_verify($passwordPost, $passwordDB)) {
        $_SESSION['loged_in'] = true;
    } else {
        $_SESSION['loged_in'] = false;
    }
    # when I do a print_r on $_SESSION['loged_in'] it results true
    header('Location:  ../../admin/index.php');

检查会话 (../../admin/index.php)

<?php 
    session_start();
    # when I do a print_r on $_SESSION['loged_in'] here, it results false
    if ($_SESSION['loged_in'] == false) {
        include(PATH_COMPONENTS.'login/index.php'); 
    }
    ?>

这怎么可能?

您必须在每个文件中启动会话!喜欢这个:

session_start();  //most times at the top of every file

是的,您必须在使用它的每个文件中启动会话!

同样对于错误报告,请使用以下命令:

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>