PHP Redirect That Works
If you tend to encounter "headers already sent" PHP warnings when doing redirects, this article is for you.
The article describes how to code PHP for a redirect that works even when headers have already been sent.
Here is the basic concept of how PHP redirects are done (or fail):
When PHP sends a page to the browser, it first sends headers. Headers generally consists of plain text lines of information that give the browser a status code, describe the type of content that follows, and other information the browser may need.
After the headers, a blank line is sent. A blank line tells the browser the headers have been sent and the rest will be content.
Then, the web page content is sent to the browser.
PHP redirect code must be in the headers. After the server has already sent the blank line, a PHP redirect won't work. PHP will issue a "headers already sent" type of warning.
This article describes a way to code a redirect so it can happen even if headers have already been sent.
What the article presents is an especially valuable technique for developers whose code might end up at any number of clients' domains and used in various ways.
Before continuing, let's clarify what is meant by and not meant by "headers".
It means the plain text lines of information PHP sends to the browser before it sends a blank line and web page content (or any other type of content, which could be an image, a PDF file, or whatever).
It does not mean the HTML head
area of a web page. The HTML head
area is web page content. Once web page content is sent to the browser, all the header lines the PHP will send are already sent.
Here's the solution:
Use the PHP headers_sent() function to test whether or not headers have already been sent. If yes, insert a JavaScript redirect command into the web page. Otherwise, use normal PHP redirect code. The code further below is an example of the header() function as utilized for normal PHP redirect code.
The reason a PHP redirect is preferable is because it's faster. The redirect takes place before any content at all is sent to the browser. But if a PHP redirect can't be done, it can still be done by inserting JavaScript into the web page.
This is code to do it with:
<?php
$URL = "http://example.com/page.php";
if( headers_sent() ) { echo("<script>location.href='$URL'</script>"); }
else { header("Location: $URL"); }
exit;
?>
Replace http://example.com/page.php with the URL of the web page where the browser is to be redirected to.
The reason to have this technique at hand, quickly available should it be needed, is because a person simply doesn't know all the situations the PHP software will be used in.
It might work just fine without the JavaScript redirect option — now. But in the future, the software might be used in a different way or the redirect code copied and pasted into different software.
Things like that aren't predictable.
It's a technique to work with the future without knowing what the future will be.
(This article first appeared in Possibilities newsletter.)
Will Bontrager