<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[PHP Forums - Header errors - read here before posting them!]]></title>
	<link rel="self" href="http://www.phpforums.org/feed-atom-topic19.xml"/>
	<updated>2009-10-10T01:27:46Z</updated>
	<generator>PunBB</generator>
	<id>http://phpforums.org/topic19-header-errors-read-here-before-posting-them.html</id>
		<entry>
			<title type="html"><![CDATA[Header errors - read here before posting them!]]></title>
			<link rel="alternate" href="http://phpforums.org/post30.html#p30"/>
			<content type="html"><![CDATA[<p>i&#039;ve seen this error pop up time and time again, and the answer is ALWAYS the same.&nbsp; please look here before you post it.&nbsp; the problem is you are outputting to the browser (whitespace included) before sending a header.&nbsp; this is unallowed.&nbsp; remove output prior to the header, or use OUTPUT BUFFERING.</p><p>NEW EDIT TO THIS TOPIC:</p><p>what usually results in these errors is poor script design.&nbsp; when one processes a form, they should do it BEFORE OUTPUTTING ANYTHING TO THE BROWSER.&nbsp; there is NO reason that the process cannot be operated in the header of the document, before any HTML is output.</p><p>let&#039;s take a common example.&nbsp; the programmer wants a login form which checks the username and password against the database.&nbsp; if they don&#039;t match, then don&#039;t set a cookie remembering them and tell them it failed.&nbsp; if they do match, set the cookie and send them to a member page.&nbsp; many will do:</p><p>[HTML starting the page and layout]<br />[PHP processing the form]<br />[form code]</p><p>because they can simply echo any errors straight from PHP right above that form.&nbsp; they can also send a success message, set the cookie, and header() them off on their merry way without seeing the form again.&nbsp; it&#039;s a logical place to put this code.&nbsp; think again.&nbsp; you&#039;ll (perhaps not so) obviously get header errors when you go to use setcookie() and header().&nbsp; if the login fails, it&#039;s dandy, but if you have a successful login the user gets nowhere.</p><p>answer:&nbsp; put the processing in the header, and store the results in variables.&nbsp; perhaps a $result variable that is 1 if successful, 0 if failed.&nbsp; then $output that contains either a success message or customized error messages.&nbsp; the new code would look like:</p><p>[PHP processing the form (if it was sent) and storing the results]<br />[HTML starting the page and layout]<br />[PHP echoing the results]<br />[form code if failed - exit(); if successful]</p><p>this will solve your header errors and will make for much more maintainable and readable code.&nbsp; if you want to redirect them elegantly (with a success message), use a &lt;meta&gt; refresh redirect.&nbsp; header() should only be used in instances where instant redirection is desired.</p>]]></content>
			<author>
				<name><![CDATA[jaydesigns]]></name>
				<uri>http://phpforums.org/user8.html</uri>
			</author>
			<updated>2009-10-10T01:27:46Z</updated>
			<id>http://phpforums.org/post30.html#p30</id>
		</entry>
</feed>

