【问题标题】:PHP redirect on submit not working提交时的PHP重定向不起作用
【发布时间】:2012-10-11 07:32:09
【问题描述】:

非常感谢您甚至尝试提出解决方案。 我在使用简单的 html 联系表格时遇到了问题。 表单的方法设置为post,它的操作被发送到php 文件。 在表单发送到的 PHP 文件的末尾,有一个基本位置:重定向。

例如)header("Location: ../index.htm");

我配置了我的 php.ini 文件 (Apache) 以显示 php 错误。 这就是我所看到的:

"Cannot modify header information - headers already sent by (output started at /home2/jwarddes/public_html/newTest/php/contact.php:2) in /home2/jwarddes/public_html/newTest/php/contact.php on line 20"

我的代码的第 20 行是我的 header("location:... 重定向。这似乎很好,但有些东西不断抛出错误。 不用说,我被难住了。

有人可以尝试一下解决方案,或者请我朝正确的方向轻推吗?

谢谢!

【问题讨论】:

  • contact.php 的第 2 行有什么内容?这就是您发送输出的地方(空格算作输出!)
  • 你有没有在 header(...) 之前回显一些东西??这可能会导致问题
  • 第 2 行只是我的开始
  • 也许你的页面中有 session_start()

标签: php html forms redirect


【解决方案1】:

我之前遇到过很多次这个问题,我在这里和那里看到过没有奏效的解决方案。在我的情况下,我把它归结为我(php include)我的页面到一个索引中,该索引已经有一个标题属性。

我解决这个问题的方法是使用元方法,echo '<meta http-equiv="refresh" content="0;url=<URL>" />';

为了使您的页面看起来更好一点,您可以添加一到两秒的延迟并执行类似...

if ($ = $) { echo '(H1)Redirecting you to...(/H1)'; echo '<meta http-equiv="refresh" content="1;url=<URL>" />'; }

【讨论】:

    【解决方案2】:

    这是一个常见的问题。

    确保您没有任何输出(即使是页面顶部的简单空格),因为这将在重定向之前发送标头。

    可能的解决方案如下。

    ob_start();
    

    在页面顶部和

    ob_end_flush(); 
    

    在底部可能会解决您的问题。

    另外,我有这个特殊的功能,它可以做一个非常智能的重定向,试试看告诉我:)

    /**
     * redirect()
     * 
     * Attempts to redirect the user to another page.
     * It tries to PHP header redirect, then JavaScript redirect, then try http redirect.
     * The following redirection method is only attempted if the previous redirection method attempt has failed  
     *  
     * @param mixed $url To be redirected to
     * @return nothing
     */
    function redirect($url)
    {
            if (!headers_sent())
            { //If headers not sent yet... then do php redirect
                    header('Location: ' . $url);
                    exit;
            } else
            { //If headers are sent... do java redirect... if java disabled, do html redirect.
                    echo '<script type="text/javascript">';
                    echo 'window.location.href="' . $url . '";';
                    echo '</script>';
                    echo '<noscript>';
                    echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
                    echo '</noscript>';
                    exit;
            }
    } //==== End -- Redirect
    

    【讨论】:

      【解决方案3】:

      在此行之前不应有任何输出(回显)发送到浏览器:

      header("位置:../index.htm");

      发布您的代码,以便我找出确切的问题

      【讨论】:

        【解决方案4】:

        从手册: PHP header()

        请记住,必须在任何实际输出之前调用 header() 通过普通的 HTML 标记、文件中的空行或 PHP 发送。 使用 include 或 require 读取代码是一个非常常见的错误, 函数,或其他文件访问函数,并且有空格或空 在调用 header() 之前输出的行。一样的问题 使用单个 PHP/HTML 文件时存在。

        【讨论】:

          【解决方案5】:

          在使用重定向之前,您不能将任何内容打印到浏览器。

          无论如何,如果你真的想这样做,你可以这样做:

          echo "<script>location.href='...';</script>"
          

          但不建议这样做,因为用户无法看到您在重定向之前打印的任何内容,那么为什么还要麻烦让它出来呢?

          【讨论】:

          • 您可以在重定向之前拥有任何东西,只要您知道标头需要先行。这通常意味着启用了输出缓冲
          【解决方案6】:

          这很可能是因为开始或结束标签的空格 检查我在工作时是否遇到了非常相似的问题...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-06
            • 2018-06-11
            • 2015-08-05
            相关资源
            最近更新 更多