Back to PHP (Applications and Scripts)

How To Resolve The PHP Warning - Headers Already Sent


This article is for cPanel Accounts. Determine where your account is with this guide.

Many PHP applications (WordPress, osCommerce, etc.) send special headers to the browser to redirect visitors to the appropriate section of the application. Applications that include multiple PHP pages to generate the output sent to the browser are prone to a problem if anything extra is added to these included pages. If you are finding that your PHP application is displaying a blank white page when it should instead be redirecting visitors elsewhere, you may be experiencing a problem with extra blank lines in one of the application's PHP source files.

For example, let's say you are seeing a blank page when you click on an "Add To Cart" button in osCommerce. With PHP error logging enabled, we see something similar to this:
Warning: Cannot modify header information - headers already sent by (output started at public_html/catalog/includes/configure.php:46) in public_html/catalog/includes/functions/general.php on line 29

What is most likely happening here is that line 46 of the public_html/catalog/includes/configure.php file contains a blank line that should not exist. We look at the configure.php page and notice that it should be one large block of PHP code, beginning with on the last line. In this case, the block of PHP code ends on line 44 and then there are two blank lines after the ?> closing PHP tag. Removing these two blank lines after the closing PHP tag will solve the problem.

What is happening is that some other page being included after configure.php (in this case, it's the page at public_html/catalog/includes/functions/general.php) is trying to send HTTP headers to the browser. Headers are generally used in PHP applications for sessions, cookies, and redirection. All HTTP headers must be sent before any page contents. If there is anything outside of a block of PHP code -- spaces, words, blank lines, anything -- then HTTP headers cannot be sent. By removing the blank lines, nothing gets sent to the browser and the HTTP headers can function properly to redirect the visitor to the correct destination.

The file and line number referenced within the ( ) will usually contain an extra blank line, but it is possible that the problem could reside elsewhere in that file or another included PHP file altogether. It is also possible that the issue could be caused by prematurely sending text or HTML code, instead of simply a blank line, so look for anything outside of tags. The referenced file and line number should be considered as a good starting point for locating the problem.



Related Articles

How To Troubleshoot PHP Upgrade Issues
Can I Enable PHP Allow_url_include On My Account
Can I Increase The PHP post_max_size Setting
Can I Modify My Suhosin Settings With A Local php.ini File
Can I Utilize PHP Shell Exec Functions

Can’t Find what you need?

No worries, Our experts are here to help.