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 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题