简体   繁体   English

本机PHP会话值在Codeigniter中丢失

[英]Native PHP Session values are lost in Codeigniter

I am working on a project that uses native PHP sessions . 我正在使用本地PHP sessions的项目中sessions I redirect user to Codeigniter page and all the session values are lost. 我将用户重定向到Codeigniter页面,所有会话值都丢失​​了。 I tried using Nativesession library, it helps me retain all the native php session data and same is true if I use session_start() in index.php of Codeigniter but I get notice messages saying " A session is active, You cannot change the session module's ini settings at this time " and " A session had already been started - ignoring session_start() " 我尝试使用Nativesession库,它可以帮助我保留所有本机php会话数据,如果我在Codeigniter的index.php中使用session_start() ,也是如此,但是我收到通知消息,说“ 会话处于活动状态,您无法更改会话模块的此时的ini设置 ”和“ 会话已经开始-忽略 session_start()

you have to start the session like this 您必须像这样开始会议

add this to your controller constructor 将此添加到您的控制器构造函数

$this->load->library('session')

you can use the session like this 您可以像这样使用会话

$this->session

add data 添加数据

$this->session->set_userdata('some_key', 'some_value');

fetch data 获取数据

$some_value = $this->session->userdata('some_key');

Read the docs 阅读文档

Codeigniter Session Management Codeigniter会话管理

After reading about how the session library works in codeigniter, figured out the root cause of the problem. 在了解了会话库如何在codeigniter中工作之后,找出了问题的根本原因。

As per documentation, 根据文档,

" When a page is loaded, the session class will check to see if valid session cookie is sent by the user's browser. If a sessions cookie does not exist (or if it doesn't match one stored on the server or has expired) a new session will be created and saved. " 加载页面后,会话类将检查用户的浏览器是否发送了有效的会话cookie。如果会话cookie不存在(或者与服务器上存储的cookie不匹配或已过期),则将创建并保存新的会话。

In my case, when I use PHP Native sessions , it doesn't create valid cookies for CI and as per above statement, CI will create new session hence old session will be destroyed. 就我而言,当我使用PHP本机会话时,它不会为CI创建有效的cookie,并且按照上述声明, CI将创建新会话,因此旧会话将被破坏。

there are two ways to get around this 有两种方法可以解决此问题

  1. suppress the Warnings and notices generated by CI session library by using @$this->load->library('session') 通过使用@$this->load->library('session')禁止CI会话库生成的警告和通知
  2. Don't load the CI session library at all and keep using Native PHP sessions. 根本不要加载CI会话库 ,而继续使用本机PHP会话。

Hope it helps if anyone is facing same issue. 希望有人遇到同样的问题会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM