1

I am trying to get the input values from the view to controller and saving this value as flashdata to use it in the other controller. Now the i am able to store the value until i redirect, but as soon as it comes to redirect, it loses its value. I am not sure whats going over here.

  • Do I need to see my config again? If yes, then what I need to see?
  • My both the functions are rights but if I am missing anything?

Here is my code below:-

Controller.php

public function first() { 
 $testing = $this->input->post('fname');
 $this->session->set_flashdata('fstname', $testing);
 $this->session->keep_flashdata('fstname');
 // echo $this->session->flashdata('fstname'); //able to get the flashdata value till here. 
 redirect('Home/second/'); //but when I am using this, flashdata loses its value
}

public function second() { 
 $data['getfname'] = $this->session->flashdata('fstname');
 $this->load->view('details', $data);
}

View (details.php)

 <?php echo $getfname ?>

Output

Null

config.php

$config['sess_driver'] = 'files'; 
$config['sess_cookie_name'] = 'ci_session'; 
$config['sess_expiration'] = 7200; 
$config['sess_save_path'] = APPPATH . 'cache/sessions/'; 
$config['sess_match_ip'] = FALSE; 
$config['sess_time_to_update'] = 300; 
$config['sess_regenerate_destroy'] = FALSE;

autoload.php

$autoload['libraries']=array();

Additionally, I also tried using userdata() but its not showing on the second function after redirect.

Thanks for your time.

Kunal Parekh
  • 380
  • 6
  • 24
  • yes, I have tried few possibibilities, but nothing work. I wil try again – Kunal Parekh Sep 11 '18 at 06:31
  • @KirkBeard i tried again, but still it didnt work – Kunal Parekh Sep 11 '18 at 06:37
  • first step to debugging, and this will be incredibly helpful throughout your coding pursuits. put `print_r`'s everywhere. first see if the post variable isn't null. then after that printr the session and see if it is there (comment out redirect for this). if it is... well then you have an issue with your session system. I've seen this type of issue with sessions getting destroyed after redirect with no known cause. I can't explain it, and AFAIK those questions were never answered. Best I can offer in that situation is a clean install of everything. – Alex Sep 11 '18 at 06:46
  • I have print_r in my codes. I just didnt copy here so that my code looks simple and easy to understand. Anyways, this is how I came to know about the value is Null – Kunal Parekh Sep 11 '18 at 06:48
  • my post variable values are not null while in the first function. I echoed the session before redirecting to another function as shown in the code above. But when I am using the redirect to second funcgtion, its value are null. And I found by using print_r and var_dump. – Kunal Parekh Sep 11 '18 at 06:51
  • @Alex so u recommend to reinstall codeigniter again? – Kunal Parekh Sep 11 '18 at 06:52
  • 1
    You aren't the first, just google "php losing session on redirect codeigniter". And like I've said, I've seen this type of question more than a few times in the past months here. I've personally experienced it but it was isolated to a particular server and was directly related with ajax calls but the problem eventually resolved itself when I upgraded to 3.1.9. Prior, I spent a whole day debugging: tried every known config, nothing changed but the site worked fine on another server so I was frustrated. You could indeed try a fresh XAMPP install/CI install. – Alex Sep 11 '18 at 06:56
  • 1
    But first try to see if regular session variables stick after redirect. Also verify you are autoloading the session library and that you have specified the session cookie name and save path. – Alex Sep 11 '18 at 06:57
  • @Alex I am using `$this->load->library('session')` in the controller under the `__Construct`. AndI am not too sure how to use the autoloading session, neither I am sure how to use the session cookies and path. – Kunal Parekh Sep 11 '18 at 07:02
  • I have left my setings basic and default – Kunal Parekh Sep 11 '18 at 07:02
  • autoload.php -> add `session` to array. check ci docs if still confused. more info on the rest of the items in the `config.php` file under `sess_` – Alex Sep 11 '18 at 07:06
  • can you share Session and cookie definitions in you config.php ? – User123456 Sep 11 '18 at 07:39
  • @BRjava I have updated my code. These are my basic settings in the config.php and autoload.php – Kunal Parekh Sep 11 '18 at 22:43
  • could you try $config['sess_save_path'] = NULL; – User123456 Sep 12 '18 at 04:43
  • @BRjava I tried with Null just now, and its same. Var_dump output is Null – Kunal Parekh Sep 12 '18 at 04:50
  • https://stackoverflow.com/questions/41210489/session-data-lost-after-redirect-in-codeigniter-3-1-2 -- can you go through these steps ? – User123456 Sep 12 '18 at 06:47
  • @BRjava I tried those steps mentioned, however I am not able to get the flashdata from one function to another after redirect. – Kunal Parekh Sep 12 '18 at 22:52
  • Possible duplicate of [CodeIgniter "flashdata" doesn't work](https://stackoverflow.com/questions/8307705/codeigniter-flashdata-doesnt-work) – saurabh kamble Sep 13 '18 at 09:57

1 Answers1

0

First:

check if Post data is not empty

$testing = $this->input->post('fname');
var_dump($testing);
exit;

Second:

you can skip keep_flashdata, you dont need that in that context

Third:

Check session setting in config.php for example:

$config['sess_driver']          = 'database';
$config['sess_cookie_name']        = 'mysession';
$config['sess_expiration']         = 86400;
$config['sess_save_path']          = 'sessions';
$config['sess_match_ip']           = TRUE;
$config['sess_time_to_update']     = 300;
$config['sess_regenerate_destroy'] = TRUE;

check autload.php: $autoload['libraries'] = array('session', [...]);

Fourth:

check session flash var directly:

$strFstname = $this->session->flashdata('fstname');
die($strFstname);

Your code should work, I used flashdata like this many times.

Community
  • 1
  • 1
Bjoern
  • 23
  • 6
  • @Bjoerm I am able to get the post values from the form (as mentioned in the first step). When I am assigning the flashdata just before the redirecting and checking its value, I am still getting the value. But as soon as after the redirect to the other function in the same controller.php file, it value gets null. I have also updated my autoload.php and config.php files as you shown here, its still not working – Kunal Parekh Sep 11 '18 at 22:53
  • and also I have removed the `keep_flashdata` from the controller. but no luck! – Kunal Parekh Sep 11 '18 at 22:54
  • My output Value (for first step) using var_dump($testing) : string(5) ''David''; – Kunal Parekh Sep 11 '18 at 23:01
  • I assume that there is a redirect before directing to 'home/second'. How are your routes managed? Maybe the system direct to the core controller and then to the home controller, then the flashdata is lost. – Bjoern Sep 13 '18 at 08:24