如何在CodeIgniter中的其他控制器中传递会话数据 [英] How to pass session data in different controller in CodeIgniter

查看:39
本文介绍了如何在CodeIgniter中的其他控制器中传递会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 verify_logins 控制器中具有以下方法:

I have the following method in my verify_loginscontroller:

function checkLogInInfo() {
    if ($_POST) {
        $v = $this->input->post('user_id');
        $vldata['password'] = $this->input->post('password');

        $query1 = $this->db->query("SELECT e.id FROM user u LEFT JOIN employee e ON u.user_id = e.id WHERE e.emp_id = '$v' ")->result_array();

        if (empty($query1)) {
            $this->session->set_flashdata('message', 'Invalid ID or Password');
            redirect('verify_logins/index');
        } else {
            $vldata = array();
            $vldata['user_id'] = $query1[0]['id'];

            $temp = $vldata['user_id'];
            $query2 = $this->db->query("SELECT e.emp_name FROM user u LEFT JOIN employee e ON u.user_id = e.id WHERE e.id ='$temp' ")->result_array();

            $vldata['user_name'] = $query2[0]['emp_name'];

            $this->session->set_userdata($vldata['user_name']);
            redirect('admin_logins/index');
        }
    } else {
        redirect('verify_logins/index');
    }
}

以上方法检查我的登录数据并登录到应用程序的主页.

The above method checks my login data and logs in to the home page of my application.

现在,我有一个名为 admin_logins 的控制器,它具有以下方法:

Now I have another controller named admin_logins with the following method:

public function index() {
    $data["message"] = $this->session->flashdata('message');
    $data["user_name"] = $this->session->userdata($vldata['user_name']);
    $this->load->view('admin_logins/index', $data);
}

以上方法加载了我的应用程序的主页.

The above method loads the home page of my application.

我正在尝试在主页中加载已登录用户的用户名.我试图将用户名从控制器verify_logins()的方法 checkLogInInfo()传递给控制器​​ admin_logins()的方法 index()使用 session ,但找不到正确的方法.我可以猜测控制器的 index()方法中存在语法问题.我需要有关正确语法和方法的帮助.

I'm trying to load the username of the logged in user in the home page. I'm trying to pass the username from method checkLogInInfo() of controller verify_logins() to method index() of controller admin_logins() by using session, but can't figure out the right way to do it. I can guess that I have some syntax problem in my index() method of the controller. I need help regarding the correct syntax and way to do it.

我试图显示用户名的视图页面部分是:

The portion of my view page where I'm trying to display the username is:

<p class="f-right">User: <strong><?php if(isset($user_name)){echo "Welcome, ".$user_name;} ?></strong></p>

我得到的错误是:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: vldata

P.S.我已经在两个控制器中加载了会话库.

P.S. I've loaded the session library in both controllers.

推荐答案

将用户名设置为类似

$this->session->set_userdata('user_name',$vldata['user_name']);

而您在检索给定为

$data["user_name"] = $this->session->userdata('user_name');

在设置会话数据时,必须将会话变量名称作为第一个参数传递,以便可以使用相同的键来检索会话数据.有关更多信息,请阅读

You have to pass the session variable name as first parameter while setting the session data so that you can use the same key to retrieve the session data.For more read this

这篇关于如何在CodeIgniter中的其他控制器中传递会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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