Codeigniter - 使用视图中的会话数据 [英] Codeigniter - using session data in view

查看:89
本文介绍了Codeigniter - 使用视图中的会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户名和姓氏存储在会话数据,我想在视图中回显它们,所以他们可以看到他们登录为。我似乎无法获得数据通过。我确信这很容易,但我对CI和整个MVC的东西很新。

I have a users first and last name stored in session data and I want to echo it out in the view so they can see who they are logged in as. I can't seem to get the data to pass. I'm sure this is something easy but I'm pretty new to CI and the whole MVC thing.

控制器代码:

public function index() {
    if($this->session->userdata('admin_signed_in')){
    $this->load->model('dashboard_model');
    $data['userdata'] = $this->session->userdata;
    $data['main_content'] = 'dashboard/dashboard';  
    $this->load->view('common/template', $data);
    } else {
    redirect('signin');
    }

}

查看代码:

<?php echo $userdata('first_name') ?>  <?php echo $userdata('last_name') ?>

打印R结果:

Array ( [session_id] => 69db0f9ccbfde96d92cc09837067438c [ip_address] => ::1 [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0 [last_activity] => 1352998508 [user_data] => [0] => Array ( [admin_id] => 10 [first_name] => Richard [last_name] => Coy [admin_role] => 1 ) [admin_signed_in] => 1 ) 


推荐答案

控制器代码:

public function index() {

    ...
    $data['first_name'] = $this->session->userdata('first_name');
    ...
}

查看代码:

<?= $first_name; ?>

或者,如果你想传递目前的数据,你将访问视图数据userdata作为数组,而不是函数,即:

OR, if you want to pass the data like you currently are, you would access the view data userdata as an array, not a function, ie:

<?= $userdata['first_name'];?>

这篇关于Codeigniter - 使用视图中的会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆