SESSION 变量在 Codeigniter php 中的 redirect() 上重置

SESSION variable is reset on redirect() in Codeigniter php

我遇到了一些奇怪的问题,会话变量在操作重定向时被重置。

我正在使用 Codeigniter 并在登录后重定向到仪表板操作,我在使用 DB 验证凭据后在登录操作中获取数据,但是当我使用 redirect() 重定向到仪表板时,会话变量消失了。

Admin.php

<?php class admin extends CI_Controller 
{
    function login()
    {
        $login = $this->Admin_model->login($this->input->post()); // <-- verify data and set to session
        if($login)
        {
            $this->session->set_flashdata("success","Logged in Successfully");
            var_dump($_SESSION); // <-- able to fetch data from session
            // exit();
            redirect("admin/dashboard");
        }
        else
        {
            $this->session->set_flashdata("error","Invalid Credentials!! Please Try Again!!");
            redirect("admin");
        }
    }

    function dashboard()
    {
        var_dump($_SESSION); // <-- session data is vanished and not able to get userdata('id')
        exit();
        if($this->session->userdata('id') != '')
        {
            $data['active_tab'] = "dashboard";
        }
        else
        {
            redirect("admin");
        }
    }
?>

Admin_model.php

<?php Class Admin_Model extends CI_Model
{
    function login($data)
    {
        $user = $this->db->get_where("users",array("username" => $data['username'],
                                     "password" => md5($data['password']),
                                     "is_active" => "1")
                                    )->row_array(); 
        if(!empty($user))
        {
            $this->set_user_session($user);
            return true;
        }
        else
        {
            return false;
        }
    }

    function set_user_session($login)
    {
        $arr = array();
        $arr["id"] = $login["id"];
        $arr["username"] = $login["username"];
        $this->session->set_userdata($arr);
    }
?>

在 xampp 和 wamp 中试过,所有浏览器,但问题仍然存在,任何帮助将不胜感激。

您必须使用 this->session->set_userdata() 来设置会话。 this->session-> set_ flashdata() 用于设置在下次操作后删除的闪现消息。

您使用的是哪个版本的 CodeIgniter?您可以尝试以下步骤。

  1. 转到system/libraries/Session/Session。php
  2. 通过添加 // 评论 session_start()。我们想重新定位 sessionn_start().
  3. 找到 (using ctrl + f) 一条说 Security is king 的评论。注释掉该注释下的所有行,直到函数结束。在我的例子中,我注释掉了行号 315 - 320.
  4. 在第 282 行将此行 ini_set('session.name', $params['cookie_name']); 更改为 ini_set('session.id', $params['cookie_name']);
  5. 注释掉以下行

    line 108 //session_set_save_handler($class, TRUE); line 290-296 // session_set_cookie_params( // $params['cookie_lifetime'], // $params['cookie_path'], // $params['cookie_domain'], // $params['cookie_secure'], // TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons // ); line 305 //ini_set('session.gc_maxlifetime', $expiration);

  6. 转到主目录index.php,它是第一个index.php,与子目录'application'、'system'、[=45位于同一目录中=],等等
  7. session_start()放在< ?php
  8. 之后

希望对您有所帮助....

由于新的 cookie 策略,新版本的浏览器可能会破坏会话。

参考文献 https://developers.google.com/search/blog/2020/01/get-ready-for-new-samesitenone-secure

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

每当需要将 cookie 发送到服务器时,浏览器会查看 SameSite 属性来决定是将 cookie 发送到服务器还是阻止。对于用户操作,它被发送到服务器,但对于自动重定向,如果 SameSite 设置为 'Strict' 或 'Lax'(Lax 现在将成为默认值)则不会。

解决方案:

可以将 cookie 属性 SameSite 设置为 'None',同时将 'Secure' 属性指定为 'true'。将 'Secure' 属性设置为 'true' 将要求您的网站在 https 上 运行。站点 运行 http:// 协议将无法设置 'Secure' cookie。 请将 'HttpOnly' 属性设置为 'true' 以使其仅供对服务器的 http 请求访问。

在PHP中可以实现如下

session_set_cookie_params(0, '/PATH/; SameSite=None', , true, true);