codeigniter 的重定向功能存在问题

发布于 2024-11-13 11:27:46 字数 1921 浏览 3 评论 0原文

这可能是一个n00b主题,但无论如何,我在这个bug上度过了一段相当困难和奇怪的时光。基本上我正在为显示表单的页面开发控制器方法。基本上,我通过使用 codeigniter 的 $this->input->post 函数 var 转储表单输入来调试表单提交的数据,如下所示:

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

每隔一天这都有效,但今天我发现当我将 post 变量转储到浏览器时,它会返回 false,就好像它们没有值一样,即使它们在我提交表单时确实有值。然后我尝试像这样直接通过 PHP 的 POST 超全局数组访问变量:

var_dump($_POST['first_name']);

并且它也返回空,所以然后我尝试像这样转储整个 post 数组:

var_dump($_POST);

尽管我填写了整个表单,但它也是空的。尽管如此,我的 MySQL 数据库中的记录正在更新(这意味着即使我的 $_POST 变量显示为空,表单仍在提交)。

另外,我推断,通常情况下,如果我在重定向函数调用之前将变量转储到控制器函数中,它应该给我一个“标头已发送”错误,但它从未这样做过。它只是将我重定向到所谓的成功页面,而不是转储我的变量。

因此,在大约 2 个小时的时间里,我认为我的 POST 数据没有被发送,并重新检查代码是否有错误,并开始一一注释掉语句,直到我能在脚本中找到罪魁祸首语句。

最后,我注释掉了一段设置成功消息和重定向的代码,如下所示:

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

然后脚本才开始使用 codeigniter 的 $this->input->post< 转储我之前的所有变量转储/code> 函数以及 $_POST 超全局数组。

好吧,如果脚本确实重定向了我,尽管变量转储在发送标头之前发送了输出,那么我可以明白为什么 $_POST 变量会显示为空。

那么真正的问题是,尽管我在发送标头之前发送了输出,但为什么脚本仍然会重定向?有人经历过吗?

任何对此的帮助将不胜感激。

编辑:关于加载视图,这是我的脚本的简化版本,如下所示 使用调试 var dump 语句:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-11-20 11:27:46

虽然我不能确定,但​​我假设您在视图文件中输出了诸如 var_dump() 之类的内容。视图不会在您调用它时执行,例如:

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

在控制器中不会导致某些视图的内容后跟“hi”。它将产生“hi”,后跟某些视图的内容。该视图实际上是在控制器中的其他所有内容运行后输出的。

这是您提供的信息中唯一想到的事情。我必须查看更多代码才能提供不同的诊断。

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.

夕色琉璃 2024-11-20 11:27:46

我遇到了“同样”的问题,我在代码的循环中发现了一个 unset() 函数。也许这会有所帮助。

I had "the same" problem and I found an unset() function in a loop in my code. Perhaps this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文