0

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.

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.

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>

And the error I'm getting is this:

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

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

1 Answer 1

3

Set the user name as like

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

And while you are retrieving give as

$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

Sign up to request clarification or add additional context in comments.

1 Comment

There is something wrong with either my PC or my browser for which I can't read anything in the CI User Guide for the past few days. That's why I couldn't go through this earlier. +1 for excellent answer and explanation. It's all crystal clear to me now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.