我试图将flashdata值从一个函数获取到同一个控制器中的另一个函数。一旦我使用redirect(),它就会丢失它的值并显示为空。我尝试使用flashdata和会话数据,但没有任何工作。请在下面找到我的代码: -
public function getUserDetails()
{
$first_name = $this->input->post('Uname');
$last_name = $this->input->post('Ulname');
//having an insert query
//I tried with session
$this->session->set_userdata('fName_sess', $first_name);
$this->session->set_userdata('lName_sess', $last_name);
//I tried with flashdata
$this->session->set_flashdata('fName_flash', $first_name);
$this->session->set_flashdata('lName_flash', $last_name);
//I am inserting the value in the database table and $uid is the PK
redirect('/User/person/'.$uid);
}
public function person()
{
//with session
$fN_sess = $this->session->userdata('fName_sess');
$lN_sess = $this->session->userdata('lName_sess');
//with keep_flashdata
$fN_flash = $this->session->keep_flashdata('fName_flash');
$lN_flash = $this->session->keep_flashdata('lName_flash');
//also tried with flashdata
$fN_flash = $this->session->flashdata('fName_flash');
$lN_flash = $this->session->flashdata('lName_flash');
echo 'Value with session'. $fN_sess . $lN_sess . '<br>';
echo 'Value with Flash' . $fN_flash . $lN_flash ;
//i have all my views loaded here
}
我不确定为什么它在重定向后没有从一个函数获取值到另一个函数。
在此先感谢您的时间。
更新:
public function getUserDetails(){
$first_name = $this->input->post('Uname');
$last_name = $this->input->post('Ulname');
//set input post to flash data
$this->session->set_flashdata('fName_flash', $first_name);
$this->session->set_flashdata('lName_flash', $last_name);
redirect('/User/person/');
}
public function person(){
$fname = $this->session->flashdata('fName_flash');
$lname = $this->session->flashdata('lName_flash');
if(isset($fname)){ // check if the flash data fname is set. you can do that to lname also.
$this->load->view(page_header);
$this->load->view(page_menu);
$this->load->view(Details);
$this->load->view(page_footer);
}
查看页面(Details.php):
<h1> Welcome <?php echo $this->session->flahdata(fName_flash).' ' . $this->session->flahdata(lName_flash) ; ?> </h1>
我在这里发现了一个帖子,它说要为sesson(click here)创建一条路径。
这是我的config.php
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
//$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE; // change this
$config['sess_match_ip'] = TRUE; // change this
$config['sess_match_useragent'] = TRUE; // change this
$config['cookie_prefix'] = '';
$config['cookie_domain'] = 'http://'. $_SERVER['HTTP_HOST'] ;
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
为了做到这一点,你需要这样做......
public function getUserDetails(){
$first_name = $this->input->post('Uname');
$last_name = $this->input->post('Ulname');
//set input post to flash data
$this->session->set_flashdata('fName_flash', $first_name);
$this->session->set_flashdata('lName_flash', $last_name);
redirect('User/person/');
}
public function person(){
$fname = $this->session->flashdata('fName_flash');
$lname = $this->session->flashdata('lName_flash');
if(isset($fname)){ // check if the flash data fname is set. you can do that to lname also.
//load views here...
//if you want to echo it inside your **view.php** file do this
$data['fname'] = $fname;
$data['lname'] = $lname;
$this->load->view('view', $data); //pass the data to view
}
然后像这样回应....
echo $fname ."<br>";
echo $lname;
//i already test it. and it's working!
希望有帮助......