I've always tried to structure code such that headers are infact the first output on a page, although occasionally its infeasible and impractical in the context of a particular design, for this reason I thought I'd share a simple workaround, it may not be the most elegant approach, but it saves lots of time, code and possible headaches.
If you had code such as the following:
Code: Select all
<?php
echo "foo";
header("Location: http://www.google.com");
?>Suppose that the code could not be changed around, lets say the header's location (in this case [url="http://www.google.com)"]http://www.google.com)[/url] wasn't actually a string URL, but was instead influenced by a variable set throughout the execution of the script. Something like the following
Code: Select all
<?php
$url = $_GET['url'];
header("Location: {$url}");
?>This case would call for output buffering (or some other way that I'm not familiar with, I never claim to be great at PHP /blum.gif' class='bbc_emoticon' alt=':P' />)
The following code will suppress any errors, and prioritise the information to be sent, such as the header command, which should be sent first.. will be cached at the beginning of the buffer, and will therefore be output first once the buffer is "flushed" if there were any echo or print statements within the code, they would follow the headers in terms of priority.
Code: Select all
<?php
ob_start();
$url = $_GET['url'];
header("Location: {$url}");
ob_end_flush();
?>If there's anything wrong here, or if you need help clarifying anything, post back!
Regards
Paul
