简体   繁体   中英

codeigniter session data in two controller methods

I have this two controller methods that both set session data

//Profile
public function profile()
{
    $this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
    $this->session->set_userdata('title', 'KYC');
    $this->load->view('basic/basic_app_views/kyc');
}

I am first loading the function kyc and using the session in my view

<title><?php echo $this->session->userdata('title'); ?></title>

I then load profile in my browser and refresh kyc but the value in kyc is still KYC like i had set in the kyc function.

How come title still has the value KYC even after resetting the value in another controller function?.

It is because each time you call your function it will set the userdata as it defines.

When try to set a title in a page, i suggest you to do this instead.

//Profile
public function profile()
{
    $data['title'] = 'Profile Page';
    $this->load->view('basic/basic_app_views/kyc', $data);
}
//KYC
public function kyc()
{
    $data['title'] = 'KYC Page';
    $this->load->view('basic/basic_app_views/kyc', $data);
}

then call it inside your kyc.php file.

<title><?php echo $title; ?></title>

hope that helps.

To see the change in the session title variable you would need to change up your code a little...

So your

//Profile
public function profile()
{
    $this->session->set_userdata('title', 'some_value');
}
//KYC
public function kyc()
{
    $this->session->set_userdata('title', 'KYC');
    $this->load->view('basic/basic_app_views/kyc');
}

To something like...

//Profile
public function profile()
{
    $this->session->set_userdata('title', 'some_value');
    $this->load->view('basic/basic_app_views/kyc');     // View the title value
}
//KYC
public function kyc()
{
    $this->session->set_userdata('title', 'KYC');
    $this->load->view('basic/basic_app_views/kyc');     // View the title value
}

That way you will see your value change. Each of your two methods individually change the value. But in your previous code only the kyc was displaying it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM