CodeIgniter flashdata 在重定向后不显示从一个函数到另一个函数的值



我正在尝试在同一控制器中将flashdata值从一个函数获取到另一个函数。一旦我使用 redirect((,它就会失去其值并显示为空。我尝试同时使用闪存数据和会话数据,但没有任何效果。请在下面找到我的代码:-

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);
}

查看页面(详细信息.php(:

<h1> Welcome <?php echo $this->session->flahdata(fName_flash).' ' . $this->session->flahdata(lName_flash) ; ?> </h1>

我在这里看到了一篇文章,上面说要为 sesson 创建一条路径 (点击这里(。

这是我的配置.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!

希望有帮助...

相关内容

  • 没有找到相关文章