Search code examples
phpcodeigniterhttp-redirectheadersuperglobals

Problem with codeigniter's redirect function


This may be a n00b topic but, anyways, I have been having a rather difficult and strange time with this bug. Basically I was working on a controller method for a page that displays a form. Basically, I was debugging the form's submitted data by var dumping the form input using codeigniter's $this->input->post function like so:

var_dump($this->input->post('first_name'));

Every other day this has worked, but today I was finding that when I dumped post variables to the browser it would return false as if they had no values even though they did have values when I submitted the form. Then I tried accessing the variables through PHP's POST superglobal array directly like so:

var_dump($_POST['first_name']);

and that returned empty as well so then I tried dumping the entire post array like so:

var_dump($_POST);

and it was empty as well despite the fact that I filled out the entire form. Nevertheless, the records in my MySQL database were being updated (which means that the form was submitting even though my $_POST variables appeared empty).

Also, I reasoned that normally, if I var dumped variables in the controller function before a redirect function call that it should give me a 'Headers already sent' error but it never did. It just redirected me to the supposed success page instead of dumping my variables.

So for the about 2 hours I thought that my POST data wasn't being sent and re-checked the code for errors and began commenting out statements one by one until I could find the culprit statement in my script.

Finally, I commented out a chunk of code that sets a success message an redirects, like so:

/*
if($record_updated_successfully)
{
    $this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
*/

and only then did the script start dumping out all my previous variable dumps using codeigniter's $this->input->post function as well as the $_POST superglobal array.

So ok, if the script does indeed redirect me despite the variable dumps sending output before headers are sent then I can see why the $_POST variables would appear empty.

So then the real question is why the script would still redirect despite my sending of output before headers are sent? Has anyone ever experienced this?

Any help with this would be appreciated.

EDIT: with respect to loading the view here's a simplified version of my script looks like with the debugging var dump statements:

function some_controller_method() {
    var_dump($this->input->post());
    var_dump($_POST);

    // some code

    if($this->input->post('form_action') == 'update record') {
        // code that runs when the form is submitted
        /*
         * ...
         */

        if($record_updated_successfully)
        {
            $this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
        }
        redirect('admin/success_page');
    }

    $this->load->view('my-view-file.php');
}

Solution

  • While I can't be sure, I'm going to assume you were outputting things like the var_dump() in your view file. A view is not executed at the time you call it, for example:

       $this->load->view('some_view');
       echo "hi!";
    

    In a controller will not result in the contents of some view followed by "hi". It will results in "hi" followed by the contents of some view. The view is actually output after everything else in the controller has run.

    This is the only thing that comes to mind with the information you've presented. I'd have to see more code to offer a different diagnosis.