我试图在同一个控制器中将 flashdata 和 userdata 的值从一个函数获取到另一个函数。似乎我在 func A 中使用重定向到 func B。所以当 func 加载时,它不会读取值并变为 Null。我使用了 var_dump() 和 print_r 但值为空。请找到我的示例代码如下: -
控制器:Sample.php
我已经在开始时声明了会话库
$this->load->library('session');
尝试使用时$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);
}
}
尝试使用时$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);
}
}
还有我的 fetchEmp.php(查看)
<h1>City: <?php echo $ct .'and department: ' . $dt ?></h1>
我得到的输出是:NULL