dongzhen4180 2016-01-11 23:38
浏览 29

headers_sent总是返回true。 无法重定向[重复]

This question already has an answer here:

I've made a login page and a logged page. The PHP code for the login page is:

<?php
  session_start();
  include 'functions.php';
  if(isValid()){
    header('Location: logged.php');
    exit;
  }
?>

and the code for the logged page is:

<?php
  session_start();
  include 'functions.php';
  if(!isValid()){
    header('Location: login.php');
    exit;
  }
?>

There is nothing before this code. After the code there is the login form in the login.php page and some informations in the logged.php page.

The isValid() function is correct, as I have tested it with an isvalid.php page that returns 1 when a user is correctly logged and 0 if there is no user logged or the informations saved in the session are wrong.

If I login with the form in the login.php page I can see all the informations in the logged.php page. If I than try to return to login.php, I am redirected to logged.php.

If I am not logged in and I try to access to logged.php the output is a blank page, without any redirect to the login.php page. PHP should redirect me to the login page!

I've seen in another discussion suggesting to debug if there is an output somewhere with headers_sent() function.

When I put a debug code like this

<?php
  if(headers_sent())
    echo "TRUE";

in the first line it returns TRUE in the logged page, nothing in the login page.

What's the problem?

</div>
  • 写回答

1条回答 默认 最新

  • duanjinchi1982 2016-01-12 00:37
    关注

    to elaborate on Fraceskix comment:

    any content outside of tags in a script is considered output and, consequently, causes headers to be sent.

    furthermore, some text editors automatically append a newline at the end. vim, for instance, does this as is outlined here. what this means is that if you have an included file that has a closing php tag it may be sending content and, thus, headers.

    the easiest way around this is to not close your php tags in files that contain only php. in fact, this is part of the psr-2 standard for this reason.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大