0

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

Kunal Parekh
  • 380
  • 6
  • 24
  • use `userdata()`, instead `flashdata()`, I guess there is no data in `$this->input->post('Ucity')` please check is not NULL before you `set_userdata()` – ichadhr Sep 07 '18 at 06:52
  • @ichadhr it is not working with userdata() either. I have tried several times by destroying the session and started it again number of times. I am not getting the data when I am redirecting it to another controller. – Kunal Parekh Sep 07 '18 at 06:58
  • verify that the post variables are not null before redirecting to narrow that down as an issue. also would be best to autoload the session lib to narrow down its placement as an issue as well as it isn't very clear where it is loaded. – Alex Sep 07 '18 at 07:42
  • Hey @Alex I checked my post variable and able to get them. I am able to store in function A as flashdata but as soon as I redirect to another function B it losses its value and displays NULL. If using redirecting, the flashdata varaible defined in function A will lose its value in function B? – Kunal Parekh Sep 09 '18 at 22:56

1 Answers1

0

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.

Kunal Parekh
  • 380
  • 6
  • 24