dongyimeng3764 2013-01-21 12:51
浏览 12
已采纳

无法修改收到的标题信息错误[重复]

Possible Duplicate:
PHP error: Cannot modify header information – headers already sent
Headers already sent by PHP

if ((isset($username)) && (isset($userid))){ 

}else{ 

header( 'Location: teacherlogout.php' ) ;

}

I have an if/else statement above, what I want to do is that if the else statement is met, then navogate to the teacherlogout.php page. But instead of this I am getting an error stating:

Warning: Cannot modify header information - headers already sent by (output started at /:431) in / on line 1654 

I don't quite get what I am doing wrong? Line 431 is just a var qremain = <?php echo (int)$_SESSION['textQuestion']; ?>; line of code. Line 1654 is the header( 'Location: teacherlogout.php' ) ; line. What is happeing here?

UPDATE:

Actually will it be better to use ajax to navigate to teacherlogout.php script in background as so:

<?php   

}else{ 

echo "Please Login to Access this Page | <a href='./teacherlogin.php'>Login</a>";

?>

<script type="text/javascript">

        $.ajax({    
        url: "teacherlogout.php",
        async: false,
        type: "POST",
      });

</script>

<?php

}

?>
  • 写回答

3条回答 默认 最新

  • dongzhizhai4070 2013-01-21 12:56
    关注
    Cause:
    

    This error is caused if the your PHP scripts are printing to the browser prior to sending headers. A common example is printing the html tags prior to starting a session, or setting a cookie. The error tells the line that needs to be altered (in this case, it is on line 431).

    Resolution:
    

    To resolve this error remove the lines from the PHP code that are printing to the browser prior to sending headers.

    Another common cause for this error is white space either at the beginning or end of the file. The fix is to remove that whitespace from the file. Read the error message carefully. It says output started at ... followed by a file name and a line number. That is the file (and line) that you need to edit. Ignore the second file name - that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看