dongzai0020 2013-10-02 14:33
浏览 87
已采纳

PHP我应该在ob_start之后使用ob_clean

I made a simple login-system in php and mysql, but I keep getting errors saying that headers already been sent, and using ob_start fixes this problem, but im not sure if I should then use ob_clean at the footer afterward?

Also, the error comes when I have logged in to the account page, saying header already been sent in previuos page - > header("Location: account.php"); But I have to redirect the user when they login.

My login page looks like this

require_once('models/init.php');  // db connection and other functions
include('header.php');  // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"


{ 

  php code to check if username/pass matches etc, and if so redirect to account page

  header("Location: account.php");

}

 echo "<form>" // display the login form

include("footer"); // including footer, some html/js code.

This code above works if I use ob_start in the header.php file. But should I use ob_clean afterwards in the footer.php file?

Sorry if anything is unclear, english is not my first languish

Thanks!

  • 写回答

2条回答 默认 最新

  • duanluwei9374 2013-10-02 14:37
    关注

    The general principle is you cannot use echo before header(). So, this will never work:

    echo "this is my header";
    header("Location: account.php");
    echo "this is my footer";
    

    However, if you sent the headers first, everything works fine:

    header("Location: account.php");
    echo "this is my header";
    echo "this is my footer";
    

    In your case, you should do the check before you include the header:

    require_once('models/init.php');  // db connection and other functions
    
    if ($user_is_logged_in) { // Do your check here
        header("Location: account.php");
    }
    
    include('header.php');  // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
    echo "<form>" // display the login form
    include("footer"); // including footer, some html/js code.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧