Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter "flashdata" doesn't work

I use CodeIgniter 2.1.0, i want after insert data in database get a message like "Your information was successfully updated.". For this work i have in CI_Controller following function:

function myCiInser(){
... Here is my query ...
//$data: this var is result query that is true
if($data){
    $this -> session -> set_flashdata('message', 'Your information was successfully updated.');
    redirect('url/myurl');
            }
}

And i have in view as:

<?php
$message = $this->session->flashdata('message');
    if($message){
        echo '<div id="error_text">' . $message . '</div>';
    }
//I test this : "echo $message;" but don't give output
?>

But i don't give message in view but redirect is done and work true. and in database in table ci_sessions column user_data i have this:

a:2:{s:9:"user_data";s:0:"";s:19:"flash:new:message";s:42:"Your information was successfully updated.";}

How can fix this problem?

UPDATE:

I had the following error (i use from chorme and by Ctrl+Shift+j i get this alert):

Failed to load resource: the server responded with a status of 404 (Not Found)

And i fix it (Now i do not have the error) but still is same problem in display message. what do i do?

like image 410
Kate Wintz Avatar asked Nov 29 '11 08:11

Kate Wintz


5 Answers

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.

like image 167
Quetzy Garcia Avatar answered Nov 20 '22 00:11

Quetzy Garcia


I had that problem too. I don't remember where I saw but here's my solution.

redirect('url/myurl','refresh');

CodeIgniter didn't treated redirect as another request. So flashdata wasn't set in the redirect, but it was on the next page I loaded.

like image 28
Vinicius Avatar answered Nov 19 '22 23:11

Vinicius


// Set flash data in our controller file

$this->session->set_flashdata('sessionkey', 'Value');

// After that we need to used redirect function

redirect("admin/signup");

// Get Flash data on view

$this->session->flashdata('sessionkey');
like image 44
Sujeet Kumar Avatar answered Nov 20 '22 00:11

Sujeet Kumar


You can also use database for the sessions, but you have to set the config items:

$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;

In that way the session flashdata will work again

like image 2
Tihomir Mihaylov Avatar answered Nov 20 '22 00:11

Tihomir Mihaylov


Using sessions with database has caused me issues at times. I recommend setting $config['sess_use_database'] = FALSE; in the config.php and see if the flashdata works fine.

like image 2
Abdulqader Kapadia Avatar answered Nov 19 '22 23:11

Abdulqader Kapadia