php - Codeigniter - issue in setting flashdata

220

This is my code in a method:


Lets say user can't access this method unless having visited another method and setting the flashdata named{-code-2} there, and then redirect to this method. so far this method works perfect as expected, but if the user make a refresh on the page ,error 404 page should appear. I want to keep the flashdata alive while user is refreshing current page so I should add a$this->session->{-code-14}('{-code-2}'); in index method (or constructor, I tried both) to do so, or i should add another$this->session->set_flashdata('{-code-2}',$pathToFile); but both solution does't work for me. Why? for testing I put avar_dump($this->session->flashdata('{-code-2}')); in the end of method . after redirect to this method I have the {-code-2} but after refresh it doesn't the value so I tried setting the flashdata I in the method and I will have it and after refreshing the page it will set to null. and also the other flashdata named{-code-6} never get the value assigned to it. why is this happening?

my session config is as bellow and I loaded the session library in{-code-7} and it works fine in other controllers and pages.

{-code-8}

333

Answer

-------

UPDATE

585

Answer

-------

A little simplicity in code:

public function test1() {
    $this->session->set_flashdata( 'img_path',"abc" );
    redirect('imgs/test2','refresh');
}

public function test2() {

    $this->session->keep_flashdata( 'img_path' );
    $pathToFile = $this->session->flashdata( 'img_path' );
    if ( $pathToFile ) {
        $this->session->set_flashdata( 'file' , $pathToFile );
        $this->session->set_flashdata( 'img_path' , $pathToFile );

        var_dump($pathToFile);
    } else {
        show_404();
    }
}

When I go tolocalhost/imgs/test1 a it will set the flashdata and redirect me tolocalhost/imgs/test2 and thevar_dump(...) section will run and prints "abc" for me in details. but when refreshing the page its show me404 error page because the flashdata is not exists anymore, even when I usedkeep_flashdata.

530

Answer

Solution:

It should work with keep_flash data. Example:

    public function test() {
      //From this request session will be set
        $this->session->set_flashdata( 'img_path',"abc" );
        redirect(base_url()."home/test");
    }

    public function index() {
    $this->session->keep_flashdata('img_path');
    $pathToFile = $this->session->flashdata( 'img_path' );
    if ( $pathToFile ) {
         $this->session->set_flashdata( 'file' , $pathToFile );
         $this->session->set_flashdata( 'img_path' , $pathToFile );

         $data[ 'pageTitle' ] = BLOGER_TITLE . ' دانلود تصاویر';
         $data[ 'is_logedin' ] = $this->is_logedin();
         $data[ 'banners' ] = $this->_getBanners( 'side' );
         $data[ 'img_name' ] = basename( $pathToFile );
         $this->load->view( 'download_image' , $data );
    } else {
        show_404();
    }

People are also looking for solutions to the problem: php - Send Acknowledgement In Mail

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.