PHP重定向提交无效

时间:2012-10-11 07:32:10

标签: php html forms redirect

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

ex)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:...重定向。这看起来很好,但有些东西一直在抛出错误。 不用说,我很难过。

有人可以尝试一下解决方案,或者让我朝着正确的方向努力吗?

谢谢!

6 个答案:

答案 0 :(得分:2)

我以前曾经多次遇到这个问题,我已经看到过这里和那里没有用过的解决方案。在我的情况下,我把我的(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>" />'; }

答案 1 :(得分:1)

在此行之前不应向浏览器发送任何输出(echo):

header(“Location:../ index.htm”);

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

答案 2 :(得分:1)

从手册中: PHP header()

  

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

答案 3 :(得分:1)

这是一个常见的问题。

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

可能的解决方案如下。

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

答案 4 :(得分:0)

在使用重定向之前,您无法在浏览器中打印任何内容。

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

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

但不推荐这样做,因为用户无法看到您重定向之前打印的内容,为什么还要打扰它呢?

答案 5 :(得分:0)

最有可能是因为起点或终点的空格 在工作时检查我有类似的问题...