Monday 25 December 2017

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



            

}else{

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


?>

type="text/javascript">

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

});





}

?>


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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...