简体   繁体   中英

Cannot get the flashdata or userdata value to another function

I am trying to get the value of the flashdata and userdata from one function to another within the same controller. It seems that I am using redirect in the func A to func B. So when the func loads it does not read the value and becomes Null. I used var_dump() and print_r but the values are null. Please find my sample code as follow:-

Controller: Sample.php

i have declared the session lib at the start

$this->load->library('session');

when tried with $this->session->userdata

public function A {

 $city = $this->input->post('Ucity');
 $depart = $this->input->post('Udepartment');

 $this->session->set_userdata('City',$city);
 $this->session->set_userdata('Department',$depart);

 //I guess the issue is coming overhere. When it redirects, it loses its values      
 redirect ('/Sample/B/');

}


public function B {

$getCity = $this->session->userdata('City');
$getDept = $this->session->userdata('Department');

  if(isset($getCity)) {
   $data['ct'] = $getCity;   
   $data['dt'] = $getDept;     

     $this->load->view(header);
     $this->load->view(menu);
     $this->load->view(fetchEmp, $data);
     $this->load->view(footer);

  }
    else {
    var_dump($getCity);
    print_r($getCity);
  }
}

when tried with $this->session->flashdata

public function A {

 $city = $this->input->post('Ucity');
 $depart = $this->input->post('Udepartment');

 $this->session->set_flashdata('City',$city);
 $this->session->set_flashdata('Department',$depart);

 $this->session->keep_flashdata('City');
 $this->session->keep_flashdata('Department');

 //I guess the issue is coming overhere. When it redirects, it loses its values    
 redirect ('/Sample/B/');

}


public function B {

$getCity = $this->session->flashdata('City');
$getDept = $this->session->flashdata('Department');

  if(isset($getCity)) {
   $data['ct'] = $getCity;   
   $data['dt'] = $getDept;     

     $this->load->view(header);
     $this->load->view(menu);
     $this->load->view(fetchEmp, $data);
     $this->load->view(footer);

  }

}

And my fetchEmp.php (view)

<h1>City: <?php echo $ct .'and department: ' . $dt ?></h1>

The output I get is: NULL

This is very simple. Flashdata will only store your value once. If you refresh the page it will then lose the data. If you not going to use the flashdata, you can then use this, and if you will be using later then just use userdata().

Step 1: go to Session.php (in Systems/Library/Session) on Line No 314 where it says:

ini_set('session.use_trans_sid',0);

comment this line by using //

Now in the same file go to line number 143 where it says:

session_start();

Comment this line as well.

Now open your controller file and right on the first line write this line:

session_start();

this needs to be very first thing on your controller.php file.

It works for me.

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