0

可能重复:
PHP 错误:无法修改标头信息 - 标头已发送 标头
已由 PHP 发送

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

}else{ 

header( 'Location: teacherlogout.php' ) ;

}

我上面有一个 if/else 语句,我想做的是,如果满足 else 语句,则导航到该teacherlogout.php页面。但不是这个,我收到一条错误消息:

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

我不太明白我做错了什么?第 431 行只是var qremain = <?php echo (int)$_SESSION['textQuestion']; ?>;一行代码。第 1654 行是该header( 'Location: teacherlogout.php' ) ;行。这里发生了什么?

更新:

实际上使用ajax在后台导航到teacherlogout.php脚本会更好,如下所示:

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

}

?>
4

3 回答 3

1

确保文件顶部没有任何空格。例如,如果您在顶部有一个空行,您将收到此错误。

(Blank Line Here)
<?php
if ((isset($username)) && (isset($userid))){ 

} else { 

header( 'Location: teacherlogout.php' ) ;

}
?>
于 2013-01-21T12:56:02.720 回答
1
Cause:

如果您的 PHP 脚本在发送标头之前打印到浏览器,则会导致此错误。一个常见的例子是在开始会话之前打印 html 标签,或者设置 cookie。该错误告诉需要更改的行(在这种情况下,它是 on line 431)。

Resolution:

要解决此错误,请在发送标头之前从 PHP 代码中删除正在打印到浏览器的行。

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.

于 2013-01-21T12:56:27.010 回答
0

header( 'Location: teacherlogout.php' ) ;

The above function (header) only works if there has been no echo (output to screen). Check if something is printed before the function call.

HTML newline (usually considered as a whitespace) may also cause problem. Like a blank new line on page start.

于 2013-01-21T13:01:32.467 回答