wildteen88 Posted June 11, 2006 Share Posted June 11, 2006 [b]Q:[/b] Why do I get the headers already sent error mesage?[b]A:[/b] The reason why you are getting this message is beacuse you have may have/be:[list][*]Whitespace before the opening php tag <?php[*]Outputting something to the browser before you use session_start, header, setcookie etc[/list]session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:[quote]Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\test.php:6) in C:\server\www\test.php on line 8[/quote]Now PHP has given us a clue here as it has told use where the output has started! Have look where it says [b]output started at[/b] and it gives you the [b]full path to the file[/b] and the [b]line number[/b]. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line. So lets look at test.php:[code] 1 <html> 2 <head> 3 <title>Header test</title> 4 </head> 5 <body> 6 <?php 7 8 header("Location: http://www.google.com"); 910 ?>11 </body>12 </html>[/code]As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!So when you get this error message again look for the clue where it says [b]output started at[/b] and goto the file and line number it says. Look above the line number and find where your output is located to.Hope that helps you understand why your are getting this error message.[b][color=red]EDIT: I found a useful website which explains how to use HTTP headers and common problems [url=http://www.expertsrt.com/tutorials/Matt/HTTP_headers.html]here[/url]. Very useful for people that dont understand headers.[/color][/b] Link to comment https://forums.phpfreaks.com/topic/11698-headers-already-sent/ Share on other sites More sharing options...
Recommended Posts