php Codeigniter Session->userdata 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12961751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:33:36  来源:igfitidea点击:

Codeigniter Session->userdata is null

phpcodeignitersession

提问by GRY

My project has several views which draw values out of the session->userdata array. the whole project works flawlessly on my development machine, but when I copied the project up to a server, I find that $this->session->userdata is null.

我的项目有几个视图,它们从 session->userdata 数组中提取值。整个项目在我的开发机器上完美运行,但是当我将项目复制到服务器时,我发现 $this->session->userdata 为空。

This is the code which inserts some values into the session->userdata array: It is called in the Controller.

这是将一些值插入 session->userdata 数组的代码:它在 Controller 中调用。

    $sessionArray = array(
            'web_id' => $id,
            'paperId' => $paperId,
            'prelimId' => $prelimId,
            'intakeId' => $intakeId,
            'finalId' => $finalId,
     );

     $this->session->set_userdata($sessionArray);

In the view file I call

在我调用的视图文件中

var_dump($this->session->userdata);

The value returned is null. As I mentioned before, the array is NOT null on my development computer (running WAMP). Does anyone know why?

返回的值为空。正如我之前提到的,我的开发计算机(运行 WAMP)上的数组不为空。有谁知道为什么?

回答by Hashem Qolami

In order to retrieve all session data as an ASSOC array, you could use the all_userdata()method. (Take a look at CodeIgniter user guide),

为了以 ASSOC 数组的形式检索所有会话数据,您可以使用该all_userdata()方法。(查看 CodeIgniter用户指南

You can write that within your Controllerand send the returned value to the view, as follows:

您可以将其写入您的控制器并将返回值发送到视图,如下所示:

$data['userdata'] = $this->session->all_userdata();
$this->load->view('your/view.php', $data); // use $userdata in the view

Note:Using the session library directly inside the viewsmay cause unexpected problems. Try managing your sessions in the Controllerinstead of the View.

注意:直接在视图内部使用会话库可能会导致意外问题。尝试在Controller而不是View 中管理您的会话。

回答by Venkata Krishna

Sorry for posting here in answer block first of all.......... Where you are assigning those values to $sessionArray. What i mean is in model or controller or in view file.

抱歉,首先在答案块中发帖.......您将这些值分配给 $sessionArray 的位置。我的意思是在模型或控制器或视图文件中。

Are you calling var_dump($this->session->userdata) in view file.

您是否在视图文件中调用 var_dump($this->session->userdata) 。

Why don't you call that in controller and pass that as an array to view file via controller.

为什么不在控制器中调用它并将其作为数组传递以通过控制器查看文件。

try like this in controller i will get user details in $login array from database for suppose when i submit username and password.

在控制器中尝试这样我将从数据库中获取 $login 数组中的用户详细信息,以供假设当我提交用户名和密码时。

I will send those details to session array like this in model file

我会将这些详细信息发送到模型文件中这样的会话数组

$this->session->set_userdata('logged_in_user', $login);

after that in controller if i want use user name from session array i will use like below.

之后在控制器中,如果我想使用会话数组中的用户名,我将使用如下所示。

$logged_in = $this->session->userdata("logged_in_user");
$model=array();
$model['logged_in_user']=$logged_in[0];

Once after i assigned to $model array i like to use that in view file so i will pass that $model array to view file from controller like this

在我分配给 $model 数组后,我喜欢在视图文件中使用它,所以我将把 $model 数组传递给控制器​​这样的视图文件

$this->load->view('view_file_path',$model);

You can use that array in view file how ever you want from passed array.

您可以在视图文件中以任何方式从传递的数组中使用该数组。