donglusou3335 2011-06-07 00:03
浏览 75
已采纳

codeigniter的重定向功能有问题

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');
}
  • 写回答

2条回答 默认 最新

  • douqiu1604 2011-06-07 00:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图