Flashdata not getting cleared in Codeigniter
Asked Answered
E

12

11

I am using Codeigniter 2.1.4 and I have facing some issues with flashdata. When I successfully submit record I can display the flashdata message. But if go to the other page from the page where flashdata message was displayed and then go back to previous page using browser back button it shows me flashdata message again.
How to clear flashdata message once it used? I think its not the flashdata issue its cache problem. I am confused why this is happening. If its cache issue then how to remove it?

Below is code I have used,

//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");

// In the view of controller
$data['message'] = $this->session->flashdata('message');

// In the view page
echo $message;
Effrontery answered 17/9, 2013 at 9:44 Comment(4)
I've read your comments on both answers, and yes you are right using back button is "never" going to give you NEW data. Please after every form submit and processing form/data redirect() somewhere so you (mostly your users) wont have to deal with "Do you want to resend data?" nag that Chrome/Firefox provides.Broad
When I use header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); in index.php it works fine. Is this good option for removing caching issue?Effrontery
I would go for "refresh" ( redirect() ) not for screwing around with headers. But you solved your problem, we do not have the big picture as you have (why is there a need for back button to work - redirect() solves this "issue").Broad
@Sachin, Did you ever found solution to this? I have spent hours but to no availHelvetian
Q
12

it's work : https://github.com/bcit-ci/CodeIgniter/pull/6013#issuecomment-1316482414

This is an issue in newer version of php . You can fix it by replacing line 420 in system/libraries/Session/Session.php:

elseif ($value < $current_time) 

with

elseif ($value === 'old' || $value < $current_time)

thanks,

Quarrelsome answered 23/2, 2023 at 23:45 Comment(1)
Pretty funny, the source code comments know they took a dumb shortcut for no reason: // Hacky, but 'old' will (implicitly) always be less than time() ;) The winky face really just...Viguerie
A
4

Flash disappears only after next refresh

Affricative answered 17/9, 2013 at 9:45 Comment(0)
S
4

Go to System->libries->Session->session.php Find flshdata fuction and replace with this

public function flashdata($key = NULL)
{
    if (isset($key))
    {
        $return= (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
            ? $_SESSION[$key]
            : NULL;
            unset($_SESSION[$key]);
            return $return;
    }

    $flashdata = array();

    if ( ! empty($_SESSION['__ci_vars']))
    {
        foreach ($_SESSION['__ci_vars'] as $key => &$value)
        {
            is_int($value) OR $flashdata[$key] = $_SESSION[$key];
        }
    }
    unset($_SESSION[$key]);
    return $flashdata;
}
Sublunar answered 21/8, 2021 at 21:42 Comment(2)
If this answer is still relevant to this 7 year old question, could you give a little bit more detail as to how this is fixing this problem. Only a block of code as a solution might be a fix, but it's not helping people learn.Afrit
Ok. What I did is each session key that is requested I reset the valueSublunar
S
3

your code in controller is right

//In the manage of controller
$this->session->set_flashdata('message', "Record updated successfully.");
redirect('controller_name/function_name','refresh');

now in view use like this

if($this->session->flashdata('message')){echo $this->session->flashdata('message');}

hope it will work

Spalding answered 17/9, 2013 at 9:50 Comment(4)
Thanks, I am sure that the issue is not of flashdata its a cache issue. Because when I click browser back button it displaying again.Effrontery
tell me one thing after setting flashdata are you redireting to any function or using $this->load->view function.Spalding
Yes, I am redirecting page to view funtionEffrontery
if you use redirect() function then second time flashdata will not show value again.Spalding
M
2

if you want to clear set_flash in controller or another view file, then you can use this simple code.

$this->session->set_flashdata('error', 'User not found...'); //create set_flash

destroy set_flash

//echo "<pre>"; print_r($_SESSION); die; //for check 

if(isset($_SESSION['error'])){
    unset($_SESSION['error']);
}
Minutiae answered 22/8, 2016 at 5:22 Comment(1)
I have a doubt here, I mostly set the flashdata in an AJAX and at success of the AJAX, I just reload the page. It shouldn't be done, right ?Abelmosk
H
2

It is php version issue. you need to modify your session file. go to your Session.php file

find the _ci_init_vars function and add this "$value === 'old' ||" in the elseif condition.

or replace the function with following.

protected function _ci_init_vars()
{
    if ( ! empty($_SESSION['__ci_vars']))
    {
        $current_time = time();

        foreach ($_SESSION['__ci_vars'] as $key => &$value)
        {
            if ($value === 'new')
            {
                $_SESSION['__ci_vars'][$key] = 'old';
            }
            // Hacky, but 'old' will (implicitly) always be less than time() ;)
            // DO NOT move this above the 'new' check!
            elseif ($value === 'old' || $value < $current_time)
            {
                unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
            }
        }

        if (empty($_SESSION['__ci_vars']))
        {
            unset($_SESSION['__ci_vars']);
        }
    }

    $this->userdata =& $_SESSION;
}
Homozygous answered 8/1, 2023 at 11:35 Comment(0)
M
1

You must redirect the page somewhere after $this->session->set_flash('item','value');

Example:

if ($this->form_validation->run() == FALSE){
    $this->session->set_flashdata('error',validation_errors());
    redirect(base_url().'user/login');
}
else{
    $this->session->set_flashdata('success','Thank you');
    redirect(base_url().'user/login');
}

Usually developer make a mistake when they submit data to same page. They set flash data but forget to redirect.

Micrometeorology answered 1/3, 2014 at 17:42 Comment(0)
A
1

Looks like this will be fixed in 3.1.12: https://github.com/bcit-ci/CodeIgniter/pull/6013

Abreaction answered 13/1, 2022 at 6:23 Comment(0)
A
0
$this->session->set_flashdata('message', "Record updated successfully.");

After setting the flashdata redirect to some function or to the same function.

Arnoldarnoldo answered 17/9, 2013 at 9:48 Comment(1)
Thanks, I am sure that the issue is not of flashdata its a cache issue. Because when I click browser back button it displaying again.Effrontery
R
0

If you refresh in the same controller function the flashdata won't be deleted.Also going back and forth in the browser does't affect the flashdata. to clear the flashdata redirect to another controller function and it will work.

Rhiamon answered 6/7, 2016 at 17:28 Comment(0)
S
0

As Code igniter does not offer the possibility to destroy the flashdata, you can work around this problem with a second fictitious call of the flashdata function without echo :

if ($this->session->flashdata('message')) :
    echo $this->session->flashdata('message'); // First normal call
    $this->session->flashdata('message'); // Second fictitious call
endif;
Sinuate answered 21/6, 2021 at 19:24 Comment(0)
B
0

I have faced same problem when I have update php version 7 to php 8. You can manually unset flash data just after display your error or success message.

$this->session->unset_userdata('err_msg')

Bodkin answered 17/1, 2023 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.