Wednesday, February 21, 2018

php - Cannot modify header information error recieved











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 = ; 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:




}else{

echo "Please Login to Access this Page | Login";

?>






}

?>

Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...