0

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

}

?>
Community
  • 1
  • 1
user1964964
  • 273
  • 2
  • 11

3 Answers3

1

Make sure there isn't any whitespace at the top of your file. For example, if you have a blank line at the top, you will get this error.

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

} else { 

header( 'Location: teacherlogout.php' ) ;

}
?>
Trevor
  • 6,659
  • 5
  • 35
  • 68
  • OP said in the question that he's outputting on line 431 and redirecting on line 1654 so nothing to do with blank lines at the moment. – Jonnix Jan 21 '13 at 12:58
  • In this case, line 431 is outputting data to the browser before the redirect. That isn't allowed. The redirect check should happen before any data is written to the client. – Trevor Jan 21 '13 at 13:00
1
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.

Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
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.

Max Shmelev
  • 3,774
  • 4
  • 26
  • 26