Search code examples
phpcodeignitercodeigniter-3

Strange behavior with flashdata in codeigniter


I have found very strange behavior of "if" condition and session flashdata in codeigniter,

public function edit_equip($equip_id, $company_id) {
        $this->data['section_title'] = 'Edit Equipment';
        $this->data['equip_detail'] = $equip_detail = $this->common->select_data_by_id('equipment', 'id', $equip_id, '*', array());
        if (empty($equip_detail)) {
            $this->session->set_flashdata('error', 'Error Ouccrred. Try Again!');
            redirect('Company/equipment/' . $company_id, 'refresh');
        }
       //some other code
        $this->load->view('company/edit_equip', $this->data);
    }

this is a function of my Equipment class. Now when I call below url like, http://localhost/scale/Equipment/edit_equip/1/2

then edit view will open correctly. but now when I press F5 button or browser refresh button then it is showing "Error Ouccrred. Try Again!" flash message which I have set in above if condition. I am not understand why this is happening, because $equip_detail contains data and I have also try to die in it but it is not going into if which is correct so why only $this->session->set_flashdata('error', 'Error Ouccrred. Try Again!'); is providing effect?

in conclusion my code is not running if block but if i press F5 or browser refresh button after first time my view is loaded it is showing me error message which is set in if condition, but my code is not going to it otherwise it has to redirect but it is loading view page with flash message.

I have only set this flash message at only one place as per above code. I am using codeigniter 3.1.6

Please can anyone explain me this?


Solution

  • This isn't the solution, but more of an investigation to satisfy yourself how things are working...

    So when things go "screwy", its always a good time to go back to basics. So seeing as you are so convinced your code is executing correctly but giving you unexpected results here is some test code just to check out the behavior of flashdata.

    Flash_session_test.php

    class Flash_session_test extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->library('session');
            $this->load->helper('url');
        }
    
        public function index()
        {
            echo "This is " . __METHOD__;
            $this->test_display();
        }
    
        /**
         * Mockup code derived from sample code
         *  to test the behavior of the flash data.
         *
         * @param $equip_id
         */
        public function edit_equip($equip_id)
        {
            echo "This is " . __METHOD__;
    
            // Set up our Fail / Pass for the If statement below.
            if (isset($equip_id)) {
                if ($equip_id == 'fail') {
                    // Create a Fail Condition
                    $equip_detail = null;
                } else {
                    // Create a Pass Condition
                    $equip_detail = array('fred' => 1);
                }
            }
            // The code we are testing.
            if (empty($equip_detail)) {
                $this->session->set_flashdata('error', 'Error Occurred. Try Again!');
                // Redirect and display the flashdata error message
                redirect('/flash_session_test', 'refresh');
            }
            $this->test_display();
        }
    
        /**
         * Our Test "View" put here for simplicity
         */
        public function test_display()
        {
            echo '<br>';
            echo '<a href="/flash_session_test/edit_equip/pass" >Click here to Create a PASS</a>';
            echo '<br>';
            echo '<a href="/flash_session_test/edit_equip/fail" >Click here to Create an ERROR</a>';
            echo '<br>';
            echo '<br>';
            // Only display the message if we have one
            $error = $this->session->flashdata('error');
            if ($error === null) { // If null, then It doesn't exist
                echo '<div style="color:green">';
                echo "Not An Error in Sight!";
                echo '</div>';
            } else {
                echo '<div style="color:red">';
                echo $this->session->flashdata('error');
                echo '</div>';
                echo "Now Refresh the page!";
            }
        }
    }
    

    Note: This will run standalone, without relying on your existing code.