【问题标题】:Session lost after page refresh [duplicate]页面刷新后会话丢失[重复]
【发布时间】:2019-05-16 16:22:33
【问题描述】:

所以我知道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']);

结果:

字符串(1)“6”

刷新后:

未定义索引:C:\xampp\htdocs\Framework\views\index.php 中的user_level_id 上线

我看过很多关于在页面刷新时丢失会话的帖子,答案是将 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();
            }
        }
    }
}

}

【问题讨论】:

    标签: php session


    【解决方案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,因为它依赖于一些自定义的会话处理

    【讨论】:

    • 我的 index.php 先生只有三行,其中包括 php 打开标签,我没有使用 CodeIgniter,但我认为问题出在重定向中
    • 我尝试创建一个新文件,该文件通过会话重定向到 index.php。它有效,也许问题出在 LoginsController.php
    • 到目前为止,您已经发布了 2 行。也许您可以编辑您的帖子以澄清第三行是什么并清楚地表明这是整个文件?您的路径/网址表明您正在使用框架,我们不知道这可能会如何干扰。
    • 我的 index.php 只包含这些行代码先生,
    • &lt;?php session_start(); var_dump($_SESSION['user_level_id']);
    猜你喜欢
    • 2014-08-16
    • 2014-07-20
    • 1970-01-01
    • 2023-03-25
    • 2017-01-13
    • 2014-12-31
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多