刷新页面后会话丢失

时间:2018-12-15 18:14:44

标签: php session

所以我知道session_start()应该在每个脚本的顶部。像这样:

logins.php

<?php
session_start();
$_SESSION['user_level_id'] = $user_level_id;
header('Location: ' . base_url('views/index.php')); 
exit();

并尝试会话:

index.php

<?php
session_start();
var_dump($_SESSION['user_level_id']);

其结果为:

  

string(1)“ 6”

并在刷新后:

  

未定义的索引:C:\ xampp \ htdocs \ Framework \ views \ index.php在行

中的user_level_id      

NULL

我已经看到很多有关在页面刷新时丢失会话的帖子,答案是将session_start()放在脚本的第一行。但似乎在我的代码中不起作用。

更新:

我尝试创建一个不包含任何内容的文件,在我以前的 logins.php 中,我包含了多个文件。所以我的新文件是这样的:

<?php
session_start();

$_SESSION['user_level_id'] = 'asdf';
session_write_close();
header('Location: /Framework/views/index.php'); 
die();

以某种方式,在index.php中,该会话未终止。我希望在登录时发布整个代码,以查看是否有任何问题。在这里:

if(isset($_POST['btn_login'])){
$username = $init->post('login_username');
$password = $init->post('login_password');

$_SESSION['logged_in'] = false;

if(!validate([$username, $password])){
    header('Location: ' . base_views('index.php?e=1'));
    exit();
} else{

    $check_username = "SELECT accounts.user_level_id , user_levels.user_level, accounts.account_id
        FROM accounts 
        JOIN user_levels 
        ON accounts.user_level_id = user_levels.user_level_id 
        WHERE username = '$username'";

    $count = $init->count($check_username);

    if($count > 1){
        header('Location: ' . base_views('index','e=2'));
        exit();
    } elseif($count === 0){
        header('Location: ' . base_views('index', 'e=3'));
        exit();
    } else{

        $sql = $init->getData($check_username);

        $user_level_id = $sql[0]->user_level_id;
        $user_level = $sql[0]->user_level;
        $account_id = $sql[0]->account_id;

        if($user_level_id === '5' || $user_level_id === '6'){
            $query = "SELECT accounts.password, students.student_id, names.fname, names.mname, names.lname, positions.position, positions.position_id
                FROM accounts 
                JOIN students ON accounts.account_id = students.account_id 
                JOIN names ON students.name_id = names.name_id 
                LEFT JOIN ssc ON ssc.students_id = students.students_id
                LEFT JOIN positions ON positions.position_id = ssc.position_id
                WHERE accounts.account_id = '$account_id'";

            $sql = $init->getData($query);

            $student_id = $sql[0]->student_id;

        } else{
            $query = "SELECT accounts.password, employees.employee_id, names.fname, names.mname, names.lname, positions.position, positions.position_id, positions.office_id, offices.office
            FROM accounts 
            JOIN employees ON employees.account_id = accounts.account_id 
            JOIN names ON employees.name_id = names.name_id 
            JOIN positions ON employees.position_id = positions.position_id 
            JOIN offices ON positions.office_id = offices.office_id
            WHERE accounts.account_id = '$account_id'";

            $sql = $init->getData($query);

            $employee_id = $sql[0]->employee_id;
            $office_id = $sql[0]->office_id;
            $office = $sql[0]->office;
        }

        $fname = $sql[0]->fname;
        $mname = $sql[0]->mname;
        $lname = $sql[0]->lname;
        $hash = $sql[0]->password;
        $position = $sql[0]->position;
        $position_id = $sql[0]->position_id;

        if(!password_verify($password, $hash)){
            header('Location: ' . base_views('index', 'e=4'));
            exit();
        } else{
            $_SESSION['user_level_id'] = $user_level_id;
            $_SESSION['user_level'] = $user_level;
            $_SESSION['account_id'] = $account_id;
            $_SESSION['full_name'] = "$fname $lname";
            $_SESSION['position'] = $position;
            $_SESSION['logged_in'] = true;

            if($user_level_id === '5'){ // Student
                header('Location: ' . base_views('student/index'));
                exit();

            } elseif($user_level_id === '6'){ // ssc
                if($position_id === '14'){
                    echo "administrator";
                } else{
                    // header('Location: ' . base_views('ssc/index'));
                    $_SESSION['user_level_id'] = 'asdf';
                    session_write_close();
                    header('Location: ' . base_url('views/loader.php'));    
                    die();
                }
            } elseif($user_level_id === '7'){ // Building Coordinator
                header('Location: ' . base_views('bldg_coordinator/index'));
                exit();

            } elseif($user_level_id === '2'){ //  Administration
                $_SESSION['office_id'] = $office_id;
                $_SESSION['office'] = $office;

                switch ($office_id) {
                    case '11': // Management Information System
                        header('Location: ' . base_views('mis/index'));
                        exit();
                        break;

                    case '12': // Plant and Facilities
                        header('Location: ' . base_views('plant_and_facilities/index'));
                        exit();
                        break;

                    case '6': // Office of Student Affairs
                        header('Location: ' . base_views('student_affairs/index'));
                        exit();
                        break;

                    case '13': // Supreme Student Council Administration
                        header('Location: ' . base_views('adviser/index'));
                        exit();
                        break;

                    default:
                        header('Location: ' . base_views('index', 'e=5'));
                        session_destroy();
                        exit();
                        break;
                }
            } else{
                session_destroy();
                header('Location: ' . base_views('index', 'e=6'));
                exit();
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

如果它是第一次工作,则表明它主要在工作-即传播了必要的cookie等。如果刷新index.php而会话值消失了,则表明该文件中的某个位置值未设置或更改。如果您不小心尝试检查该值,但使用了一个等号=而不是两个==,则可能会发生这种情况:

if ($_SESSION["user_level_id"] = NULL) { // this actually sets the value to null
    // BLAH BLAH BLAH
}

或者您可能已经做到了:

unset($_SESSION["user_level_id"]);

如果您没有任何可能进行此更改的代码,则表明该会话可能丢失了。当会话cookie丢失(或以某种方式更改)时,可能会发生这种情况。您是否愿意共享更多代码?

编辑:其他可能性:

  • 从http重定向到https可能会导致会话丢失
  • 会话可能已过期。如果您的会话过期时间很短,或者您打开浏览器并在很长一段时间后又恢复工作,则可能会发生这种情况
  • 如果您使用codeigniter,请考虑自动加载(或显式加载)会话库,而不要调用session_start,因为它依赖于某些自定义的会话处理