douwei7976 2012-10-11 07:32
浏览 73
已采纳

PHP重定向提交无效

Many thanks in advance for even attempting to present a solution. I'm having trouble with a simple html contact form. The form's method is set to post and it's action is sent to a php file. At the end of the PHP file the form is sent to, there is a basic location:redirection.

ex) header("Location: ../index.htm");

I configured my php.ini file (Apache) to display the php error(s). This is what I'm shown:

"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"

Line 20 of my code is my header("location:... redirect. This appears to be fine, yet something keeps throwing an error. Needless to say, I'm stumped.

Could someone please try their hand at a solution or kindly nudge me in the right direction?

Thanks!

  • 写回答

6条回答 默认 最新

  • duanbiao4025 2012-10-11 07:43
    关注

    that is a common problem.

    make sure you do not have any output (even a simple space at the top of the page) as that will send the headers prior to your redirect.

    Possible Solutions are the following.

    ob_start();
    

    in the top of your page and

    ob_end_flush(); 
    

    at the bottom might solve your issue.

    Also, I have this special function below which do a very smart redirect, try it out and let me know :)

    /**
     * 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑