Codeigniter "Flashdata" Doesn't Work

codeigniter flashdata not working as expected

Finally it works after a long effort. All you have to do is use $this->session->keep_flashdata('message') with $this->session->unset_userdata('message')

here is my solution (view file)

    <?php
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-info">' . $message . '</div>';
$this->session->unset_userdata('message');
}

?>

After that in my controller construct function

 public function __construct() {
parent::__construct();
.....
$this->session->keep_flashdata('message');
}

it works in each error. still have some silly issue but working nicely so far

CodeIgniter flashdata doesn't work

From the Codeigniter Session Class documentation, regarding Flashdata we can read:

CodeIgniter supports "flashdata", or session data that will only be
available for the next server request, and are then automatically
cleared.

Your problem might be that when you redirect, the process takes more than one request, clearing your flashdata.

To see if that's the case, just add the following code to the constructor of the controller you are redirecting to:

$this->session->keep_flashdata('message');

This will keep the flashdata for another server request, allowing it to be used afterwards.

$this-session-set_flashdata() and then $this-session-flashdata() doesn't work in codeigniter

Well, the documentation does actually state that

CodeIgniter supports "flashdata", or session data that will only be
available for the next server request, and are then automatically
cleared.

as the very first thing, which obviusly means that you need to do a new server request.
A redirect, a refresh, a link or some other mean to send the user to the next request.

Why use flashdata if you are using it in the same request, anyway? You'd might as well not use flashdata or use a regular session.

CodeIgniter Flashdata doesn't work on server

It doesn't. Something strange is going on with your server.

Check that that a database connection is being made if you're using database sessions.

Check that what you're redirecting to is actually the same server.

Check that you're using flashdata correctly. (Post code?)

Check that session variables are working at all.

Update:

// Again, be sure that 'session' is autoloaded in config.php
class Test_Controller {
public function page1()
{
$this->session->set_flashdata('test', 'HELLO WORLD');
redirect('test/page2');
}
public function page2()
{
echo $this->session->flashdata('test');
}
}

Add this controller and visit http://yourserver.com/index.php/test/page1

It should redirect to page2 and display 'HELLO WORLD'.

If it does, then it's a problem with your code. If it doesn't, then I would suggest loading up a virtual machine with an OS and installing a new copy of a webserver and trying again.



Related Topics



Leave a reply



Submit