Codeigniter - issue in setting flashdata


Problem:

This is my code in a method:

public function index() {
    $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();
    }
}

Lets say user can't access this method unless having visited another method and setting the flashdata named img_path 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->keep_flashdata('img_path'); in index method (or constructor, I tried both) to do so, or i should add another $this->session->set_flashdata('img_path',$pathToFile); but both solution does't work for me. Why? for testing I put a var_dump($this->session->flashdata('img_path')); in the end of method . after redirect to this method I have the img_path 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 file never get the value assigned to it. why is this happening?

my session config is as bellow and I loaded the session library in autoload.php and it works fine in other controllers and pages.

$config['sess_expiration'] = 0;
$config['sess_save_path'] = APPPATH . 'cache/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

-------------------------------------

UPDATE

-------------------------------------

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 to localhost/imgs/test1 a it will set the flashdata and redirect me to localhost/imgs/test2 and the var_dump(...) section will run and prints "abc" for me in details. but when refreshing the page its show me 404 error page because the flashdata is not exists anymore, even when I used keep_flashdata.


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();
    }

Recent Tips

  1. JQuery UI Dialog: Dynamic OnClick Listener
  2. Can´t access google cloud storage with service account. Service account does not have storage.objects.list access
  3. link error to build caffe in osx 10.9
  4. How to use CSS to give a circle a "partial border"
  5. BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.1.195:85/tranxavApi/public/api/signup
  6. Laravel 5.1 Authentication without password
  7. django-bootstrap-v5 DecimalField Throwing Errors in ModelForm Class
  8. update and delete in realm xamarin
  9. Grabbing text between Processing Instructions in XSLT
  10. Foreign key refering to multiple tables
  11. Run a visual studio 2010 Windows7 project on a Windows XP machine
  12. How to apply groupname to HTML radio buttons in asp.net?
  13. What is the most stable modal dialog implementation across browsers to use from javascript for a web app?
  14. Move, Remove, and Replace information from Google Sheets with new entries using Google Script
  15. How can I get rid of jerkiness in WinForms scrolling animation?
  16. Update MySQL table column from another table entities
  17. Shoot fireball once in unity C#
  18. Adding a table of contents to a Microsoft Word Document using vbs
  19. Change locale in android app (onto Hindi)
  20. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  21. Sharepoint with silverlight app
  22. What's the best way to write robots.txt for github pages using multiple repos?
  23. Biopython: Cant use .count() for biopython
  24. How can I find out the token balance of an address?
  25. ref value is undefined in vue (modal, textarea, $refs)
  26. Azure - HDInsight Hbase Data Insertion Failed
  27. SignalR overwriting OnConnected(), OnDisconnected()
  28. DatePickerDialog displays with two borders
  29. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  30. hiding the autocomplete list when user click outside the textbox is not working as expected