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

时间:2018-09-06 23:13:27

标签: codeigniter codeigniter-3 codeigniter-2 codeigniter-session codeigniter-flashdata

我正在尝试将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>

我在这里遇到了一条帖子,上面说要为塞森创建一条路 (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;

1 个答案:

答案 0 :(得分:0)

为此,您需要这样做...

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!

希望有帮助...