doxn43207 2015-01-15 06:50
浏览 55

只有控制器的某些功能无法访问codeigniter中的会话数据

i am new to codeigniter. i am setting the session in one controller. and i could access that session data from some functions of another controller. but only some functions cannot access the session data. please help me why this is happening.

i set the session by

$admin_details=array(
    'admin_id'=>$admin_id,
    'admin_name'=>$admin_name,
    'email'=>$email,
    'is_admin_logged_in'=>true,
    'user_type'=>'admin'
    );
$this->session->set_userdata($admin_details);

and i accessed it by

function manage_profile()
{

    if(!$this->session->userdata('is_admin_logged_in'))
    {
        $this->load->view('admin/test');
    }
}

but it is loading the view 'test'. i have loaded the session library and set the encryption key. it works in some other functions in the same controller.

  • 写回答

2条回答 默认 最新

  • doumi1311 2015-01-15 08:02
    关注

    It's because when you type $this->session->userdata('is_admin_logged_in') You're accessing the 'container' for the session.

    $admin_details=array( 'admin_id'=>$admin_id, 'admin_name'=>$admin_name, 'email'=>$email, 'is_admin_logged_in'=>true, 'user_type'=>'admin' ); $this->session->set_userdata($admin_details);

    You actually need to do this: $this->session->set_userdata('name_your_session_something',$admin_details);

    Then to access it:

    $session = $this->session->userdata('name_your_session_something'); $admin_logged = $session['is_admin_logged_in'];

    if(!$admin_logged){ //if admin is not logged in do this }

    It's a simple array situation.

    So this should be the code:

    Set Session:

    $admin_details=array( 
            'admin_id'=>$admin_id, 
            'admin_name'=>$admin_name, 
            'email'=>$email, 
            'is_admin_logged_in'=>true, 
            'user_type'=>'admin' 
    );
    $this->session->set_userdata('name_your_session_something',$admin_details);
    

    Get Session:

    $session = $this->session->userdata('name_your_session_something'); 
    $admin_logged = $session['is_admin_logged_in'];
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?