{"id":946,"date":"2022-08-20T21:33:54","date_gmt":"2022-08-20T16:03:54","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-how-to-fix-headers-already-sent-error-in-php\/"},"modified":"2022-08-20T21:33:54","modified_gmt":"2022-08-20T16:03:54","slug":"solved-how-to-fix-headers-already-sent-error-in-php-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/","title":{"rendered":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>Headers already sent error is a common issue encountered by PHP developers. This error occurs when the server sends the response to the browser before the header information is sent. This can be caused by a number of things, such as extra whitespace, incorrect line endings, or even a missing semicolon. Fortunately, this error can be easily fixed by following a few simple steps. In this article, we will discuss how to fix the \u201cHeaders already sent\u201d error in PHP. We will cover the causes of this error, how to identify it, and the steps to take to fix it. By the end of this article, you should have a better understanding of how to troubleshoot and fix this error.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>The &#8220;Headers already sent&#8221; error in PHP is caused by having any output (such as HTML or whitespace) before the header() function is called. To fix this error, you must ensure that no output is sent before the header() function is called.<\/p>\n<p>This can be done by removing any whitespace or HTML before the header() function, or by using output buffering. Output buffering allows you to capture all output before it is sent to the browser, and then send it all at once. This can be done by adding the following line of code at the top of your PHP script:<\/p>\n<p>ob_start();<\/p>\n<p>This will start output buffering, and all output will be stored in a buffer until the ob_end_flush() function is called. Once the ob_end_flush() function is called, all output will be sent to the browser. <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><\/p>\n<p><script><\/p>\n<p><\/script><\/p>\n<p>\n<\/p>\n<div id=\"answer-8028987\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"8028987\" data-parentid=\"8028957\" data-score=\"3248\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h2>No output before sending headers!<\/h2>\n<p>Functions that send\/modify HTTP headers must be invoked <em><strong>before any output is made<\/strong><\/em>.<br \/>\n<kbd><strong>summary \u21ca<\/strong><\/kbd><br \/>\nOtherwise the call fails:<\/p>\n<blockquote>\n<p>Warning: Cannot modify header information \u2013 headers already sent (output started at <i>script:line<\/i>)<\/p>\n<\/blockquote>\n<p>Some functions modifying the HTTP header are:<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/header\"><code>header<\/code><\/a> \/ <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/header_remove\"><code>header_remove<\/code><\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/session_start\"><code>session_start<\/code><\/a> \/ <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/session_regenerate_id\"><code>session_regenerate_id<\/code><\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/setcookie\"><code>setcookie<\/code><\/a> \/ <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/setrawcookie\"><code>setrawcookie<\/code><\/a><\/li>\n<\/ul>\n<p>Output can be:<\/p>\n<ul>\n<li>\n<p><em>Unintentional:<\/em><\/p>\n<ul>\n<li>Whitespace before <code>&lt;?php<\/code> or after <code>?&gt;<\/code><\/li>\n<li>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\">UTF-8 Byte Order Mark<\/a> specifically<\/li>\n<li>Previous error messages or notices<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p><em>Intentional:<\/em><\/p>\n<ul>\n<li><code>print<\/code>, <code>echo<\/code> and other functions producing output<\/li>\n<li>Raw <code>&lt;html&gt;<\/code> sections prior <code>&lt;?php<\/code> code.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Why does it happen?<\/h2>\n<p>To understand why headers must be sent before output it\u2019s necessary<br \/>\nto look at a typical <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Hypertext_Transfer_Protocol\">HTTP<\/a><br \/>\nresponse. PHP scripts mainly generate HTML content, but also pass a<br \/>\nset of HTTP\/CGI headers to the webserver:<\/p>\n<pre><code>HTTP\/1.1 200 OK\nPowered-By: PHP\/5.3.7\nVary: Accept-Encoding\nContent-Type: text\/html; charset=utf-8\n\n&lt;html&gt;&lt;head&gt;&lt;title&gt;PHP page output page&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;&lt;h1&gt;Content&lt;\/h1&gt; &lt;p&gt;Some more output follows...&lt;\/p&gt;\nand &lt;a href=\"https:\/\/jassweb.com\/\"&gt; &lt;img src=internal-icon-delayed&gt; &lt;\/a&gt;\n<\/code><\/pre>\n<p>The page\/output always <em>follows<\/em> the headers. PHP has to pass the<br \/>\nheaders to the webserver first. It can only do that once.<br \/>\nAfter the double linebreak it can nevermore amend them.<\/p>\n<p>When PHP receives the first output (<code>print<\/code>, <code>echo<\/code>, <code>&lt;html&gt;<\/code>) it will<br \/>\n<em>flush<\/em> all collected headers. Afterward it can send all the output<br \/>\nit wants. But sending further HTTP headers is impossible then.<\/p>\n<h2>How can you find out where the premature output occurred?<\/h2>\n<p>The <code>header()<\/code> warning contains all relevant information to<br \/>\nlocate the problem cause:<\/p>\n<blockquote>\n<p>Warning: Cannot modify header information \u2013 headers already sent by<br \/>\n<em><strong>(output started at<\/strong><\/em> \/www\/usr2345\/htdocs\/<b>auth.php:52<\/b>) in<br \/>\n\/www\/usr2345\/htdocs\/index.php on line 100<\/p>\n<\/blockquote>\n<p>Here \u201cline 100\u201d refers to the script where the <code>header()<\/code> <em>invocation<\/em> failed.<\/p>\n<p>The \u201c<em>output started at<\/em>\u201d note within the parenthesis is more significant.<br \/>\nIt denominates the source of previous output. In this example, it\u2019s <code>auth.php<\/code><br \/>\nand <strong>line <code>52<\/code><\/strong>. That\u2019s where you had to look for premature output.<\/p>\n<p><em>Typical causes:<\/em><\/p>\n<ol>\n<li>\n<h3>Print, echo<\/h3>\n<p>Intentional output from <code>print<\/code> and <code>echo<\/code> statements will terminate the opportunity to send HTTP headers. The application flow must be restructured to avoid that. Use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/function\">functions<\/a><br \/>\nand templating schemes. Ensure <code>header()<\/code> calls occur <em>before<\/em> messages<br \/>\nare written out.<\/p>\n<p>Functions that produce output include<\/p>\n<ul>\n<li><code>print<\/code>, <code>echo<\/code>, <code>printf<\/code>, <code>vprintf<\/code><\/li>\n<li><code>trigger_error<\/code>, <code>ob_flush<\/code>, <code>ob_end_flush<\/code>, <code>var_dump<\/code>, <code>print_r<\/code><\/li>\n<li><code>readfile<\/code>, <code>passthru<\/code>, <code>flush<\/code>, <code>imagepng<\/code>, <code>imagejpeg<\/code><\/li>\n<\/ul>\n<p> among others and user-defined functions.<\/p>\n<\/li>\n<li>\n<h3>Raw HTML areas<\/h3>\n<p>Unparsed HTML sections in a <code>.php<\/code> file are direct output as well.<br \/>\nScript conditions that will trigger a <code>header()<\/code> call must be noted<br \/>\nbefore <em>any<\/em> raw <code>&lt;html&gt;<\/code> blocks.<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;?php\n    \/\/ Too late for headers already.\n<\/code><\/pre>\n<p>Use a templating scheme to separate processing from output logic.<\/p>\n<ul>\n<li>Place form processing code atop scripts.<\/li>\n<li>Use temporary string variables to defer messages.<\/li>\n<li>The actual output logic and intermixed HTML output should follow last.\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h3>Whitespace before <code>&lt;?php<\/code> for \u201cscript.php <strong>line 1<\/strong>\u201d warnings<\/h3>\n<p>If the warning refers to output inline <strong><code>1<\/code><\/strong>, then it\u2019s mostly<br \/>\nleading <strong>whitespace<\/strong>, text or HTML before the opening <code>&lt;?php<\/code> token.<\/p>\n<pre><code> &lt;?php\n# There's a SINGLE space\/newline before &lt;? - Which already seals it.\n<\/code><\/pre>\n<p>Similarly it can occur for appended scripts or script sections:<\/p>\n<pre><code>?&gt;\n\n&lt;?php\n<\/code><\/pre>\n<p>PHP actually eats up a <em>single<\/em> linebreak after close tags. But it won\u2019t<br \/>\ncompensate multiple newlines or tabs or spaces shifted into such gaps.<\/p>\n<\/li>\n<li>\n<h3>UTF-8 BOM<\/h3>\n<p>Linebreaks and spaces alone can be a problem. But there are also \u201cinvisible\u201d<br \/>\ncharacter sequences that can cause this. Most famously the<br \/>\n<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\"><strong>UTF-8 BOM<\/strong> (Byte-Order-Mark)<\/a><br \/>\nwhich isn\u2019t displayed by most text editors. It\u2019s the byte sequence <code>EF BB BF<\/code>, which is optional and redundant for UTF-8 encoded documents. PHP however has to treat it as raw output. It may show up as the characters <code>\u00ef\u00bb\u00bf<\/code> in the output (if the client interprets the document as Latin-1) or similar \u201cgarbage\u201d.<\/p>\n<p>In particular graphical editors and Java-based IDEs are oblivious to its<br \/>\npresence. They don\u2019t visualize it (obliged by the Unicode standard).<br \/>\nMost programmer and console editors however do:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\" width=\"590\" height=\"140\" alt=\"joes editor showing UTF-8 BOM placeholder, and MC editor a dot\"><\/p>\n<p>There it\u2019s easy to recognize the problem early on. Other editors may identify<br \/>\nits presence in a file\/settings menu (Notepad++ on Windows can identify and<br \/>\nremedy the problem),<br \/>\nAnother option to inspect the BOMs presence is resorting to an <strong>hexeditor<\/strong>.<br \/>\nOn *nix systems <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/linux.die.net\/man\/1\/hexdump\"><code>hexdump<\/code><\/a> is usually available,<br \/>\nif not a graphical variant which simplifies auditing these and other issues:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/1661011434_74_Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\" width=\"560\" height=\"87\" alt=\"beav hexeditor showing utf-8 bom\"><\/p>\n<p>An easy fix is to set the text editor to save files as \u201cUTF-8 (no BOM)\u201d<br \/>\nor similar to such nomenclature. Often newcomers otherwise resort to creating new files and just copy&amp;pasting the previous code back in.<\/p>\n<h3>Correction utilities <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.gif\" width=\"30\" height=\"20\"><\/h3>\n<p>There are also automated tools to examine and rewrite text files<br \/>\n(<code>sed<\/code>\/<code>awk<\/code> or <code>recode<\/code>).<br \/>\nFor PHP specifically there\u2019s the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/freshcode.club\/projects\/phptags\"><code>phptags<\/code> tag tidier<\/a>.<br \/>\nIt rewrites close and open tags into long and short forms, but also easily<br \/>\nfixes leading and trailing whitespace, Unicode and UTF-x BOM issues:<\/p>\n<pre><code>phptags  --whitespace  *.php\n<\/code><\/pre>\n<p>It\u2019s safe to use on a whole include or project directory.<\/p>\n<\/li>\n<li>\n<h3>Whitespace after <code>?&gt;<\/code><\/h3>\n<p>If the error source is mentioned as behind the<br \/>\nclosing <code>?&gt;<\/code><br \/>\nthen this is where some whitespace or the raw text got written out.<br \/>\nThe PHP end marker does not terminate script execution at this point. Any text\/space characters after it will be written out as page content<br \/>\nstill.<\/p>\n<p>It\u2019s commonly advised, in particular to newcomers, that trailing <code>?&gt;<\/code> PHP<br \/>\nclose tags should be omitted. This <em>eschews<\/em> a small portion of these cases.<br \/>\n(Quite commonly <code>include()d<\/code> scripts are the culprit.)<\/p>\n<\/li>\n<li>\n<h3>Error source mentioned as \u201cUnknown on line 0\u201d<\/h3>\n<p>It\u2019s typically a PHP extension or php.ini setting if no error source<br \/>\nis concretized.<\/p>\n<ul>\n<li>It\u2019s occasionally the <code>gzip<\/code> stream encoding setting<br \/>\nor the <code>ob_gzhandler<\/code>.<\/li>\n<li>But it could also be any doubly loaded <code>extension=<\/code> module<br \/>\ngenerating an implicit PHP startup\/warning message.\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<h3>Preceding error messages<\/h3>\n<p>If another PHP statement or expression causes a warning message or<br \/>\nnotice being printed out, that also counts as premature output.<\/p>\n<p>In this case you need to eschew the error,<br \/>\ndelay the statement execution, or suppress the message with e.g.<br \/>\n<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/isset\"><code>isset()<\/code><\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/@\"><code>@()<\/code><\/a> \u2013<br \/>\nwhen either doesn\u2019t obstruct debugging later on.<\/p>\n<\/li>\n<\/ol>\n<h2>No error message<\/h2>\n<p>If you have <code>error_reporting<\/code> or <code>display_errors<\/code> disabled per <code>php.ini<\/code>,<br \/>\nthen no warning will show up. But ignoring errors won\u2019t make the problem go<br \/>\naway. Headers still can\u2019t be sent after premature output.<\/p>\n<p>So when <code>header(\"Location: ...\")<\/code> redirects silently fail it\u2019s very<br \/>\nadvisable to probe for warnings. Reenable them with two simple commands<br \/>\natop the invocation script:<\/p>\n<pre><code>error_reporting(E_ALL);\nini_set(\"display_errors\", 1);\n<\/code><\/pre>\n<p>Or <code>set_error_handler(\"var_dump\");<\/code> if all else fails.<\/p>\n<p>Speaking of redirect headers, you should often use an idiom like<br \/>\nthis for final code paths:<\/p>\n<pre><code>exit(header(\"Location: \/finished.html\"));\n<\/code><\/pre>\n<p>Preferably even a utility function, which prints a user message<br \/>\nin case of <code>header()<\/code> failures.<\/p>\n<h2>Output buffering as a workaround<\/h2>\n<p>PHPs <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/intro.outcontrol.php\">output buffering<\/a><br \/>\nis a workaround to alleviate this issue. It often works reliably, but shouldn\u2019t<br \/>\nsubstitute for proper application structuring and separating output from control<br \/>\nlogic. Its actual purpose is minimizing chunked transfers to the webserver.<\/p>\n<ol>\n<li>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/outcontrol.configuration.php\"><code>output_buffering=<\/code><\/a><br \/>\nsetting nevertheless can help.<br \/>\nConfigure it in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/configuration.file.php\">php.ini<\/a><br \/>\nor via <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/configuration.changes.php\">.htaccess<\/a><br \/>\nor even <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/configuration.file.per-user.php\">.user.ini<\/a> on<br \/>\nmodern FPM\/FastCGI setups.<br \/>\nEnabling it will allow PHP to buffer output instead of passing it to the webserver instantly. PHP thus can aggregate HTTP headers.<\/p>\n<\/li>\n<li>\n<p>It can likewise be engaged with a call to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/ob_start\"><code>ob_start();<\/code><\/a><br \/>\natop the invocation script. Which however is less reliable for multiple reasons:<\/p>\n<ul>\n<li>\n<p>Even if <code>&lt;?php ob_start(); ?&gt;<\/code> starts the first script, whitespace or a<br \/>\nBOM might get shuffled before, rendering it ineffective.<\/p>\n<\/li>\n<li>\n<p>It can conceal whitespace for HTML output. But as soon as the application logic attempts to send binary content (a generated image for example),<br \/>\nthe buffered extraneous output becomes a problem. (Necessitating <code>ob_clean()<\/code><br \/>\nas a further workaround.)<\/p>\n<\/li>\n<li>\n<p>The buffer is limited in size, and can easily overrun when left to defaults.<br \/>\nAnd that\u2019s not a rare occurrence either, difficult to track down<br \/>\nwhen it happens.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Both approaches therefore may become unreliable \u2013 in particular when switching between<br \/>\ndevelopment setups and\/or production servers. This is why output buffering is<br \/>\nwidely considered just a crutch \/ strictly a workaround.<\/p>\n<p>See also the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.php.net\/manual\/en\/outcontrol.examples.basic.php\">basic usage example<\/a><br \/>\nin the manual, and for more pros and cons:<\/p>\n<ul>\n<li>What is output buffering?<\/li>\n<li>Why use output buffering in PHP?<\/li>\n<li>Is using output buffering considered a bad practice?<\/li>\n<li>Use case for output buffering as the correct solution to \u201cheaders already sent\u201d<\/li>\n<\/ul>\n<h3>But it worked on the other server!?<\/h3>\n<p>If you didn\u2019t get the headers warning before, then the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/outcontrol.configuration.php\">output buffering<br \/>\nphp.ini setting<\/a><br \/>\nhas changed. It\u2019s likely unconfigured on the current\/new server.<\/p>\n<h2>Checking with <code>headers_sent()<\/code><\/h2>\n<p>You can always use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/headers_sent\"><code>headers_sent()<\/code><\/a> to probe if<br \/>\nit\u2019s still possible to\u2026 send headers. Which is useful to conditionally print<br \/>\ninfo or apply other fallback logic.<\/p>\n<pre><code>if (headers_sent()) {\n    die(\"Redirect failed. Please click on this link: &lt;a href=...&gt;\");\n}\nelse{\n    exit(header(\"Location: \/user.php\"));\n}\n<\/code><\/pre>\n<p>Useful fallback workarounds are:<\/p>\n<ul>\n<li>\n<h3>HTML <code>&lt;meta&gt;<\/code> tag<\/h3>\n<p>If your application is structurally hard to fix, then an easy (but<br \/>\nsomewhat unprofessional) way to allow redirects is injecting a HTML<br \/>\n<code>&lt;meta&gt;<\/code> tag. A redirect can be achieved with:<\/p>\n<pre><code> &lt;meta http-equiv=\"Location\" content=\"http:\/\/example.com\/\"&gt;\n<\/code><\/pre>\n<p>Or with a short delay:<\/p>\n<pre><code> &lt;meta http-equiv=\"Refresh\" content=\"2; url=..\/target.html\"&gt;\n<\/code><\/pre>\n<p>This leads to non-valid HTML when utilized past the <code>&lt;head&gt;<\/code> section.<br \/>\nMost browsers still accept it.<\/p>\n<\/li>\n<li>\n<h3>JavaScript redirect<\/h3>\n<p>As alternative a JavaScript redirect<br \/>\ncan be used for page redirects:<\/p>\n<pre><code> &lt;script&gt; location.replace(\"target.html\"); &lt;\/script&gt;\n<\/code><\/pre>\n<p>While this is often more HTML compliant than the <code>&lt;meta&gt;<\/code> workaround,<br \/>\nit incurs a reliance on JavaScript-capable clients.<\/p>\n<\/li>\n<\/ul>\n<p>Both approaches however make acceptable fallbacks when genuine HTTP header()<br \/>\ncalls fail. Ideally you\u2019d always combine this with a user-friendly message and<br \/>\nclickable link as last resort. (Which for instance is what the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/php.uz\/manual\/en\/function.http-redirect.php\">http_redirect()<\/a><br \/>\nPECL extension does.)<\/p>\n<h2>Why <code>setcookie()<\/code> and <code>session_start()<\/code> are also affected<\/h2>\n<p>Both <code>setcookie()<\/code> and <code>session_start()<\/code> need to send a <code>Set-Cookie:<\/code> HTTP header.<br \/>\nThe same conditions therefore apply, and similar error messages will be generated<br \/>\nfor premature output situations.<\/p>\n<p>(Of course, they\u2019re furthermore affected by disabled cookies in the browser<br \/>\nor even proxy issues. The session functionality obviously also depends on free<br \/>\ndisk space and other php.ini settings, etc.)<\/p>\n<h2>Further links<\/h2>\n<ul>\n<li>Google provides a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.google.com\/search?q=php+headers+already+sent\">lengthy list of similar discussions<\/a>.<\/li>\n<li>And of course many specific cases have been covered on Stack Overflow as well.<\/li>\n<li>The WordPress FAQ explains <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F\">How do I solve the Headers already sent warning problem?<\/a> in a generic manner.<\/li>\n<li>Adobe Community: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/kb2.adobe.com\/community\/publishing\/505\/cpsid_50572.html\">PHP development: why redirects don\u2019t work (headers already sent)<\/a><\/li>\n<li>Nucleus FAQ: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/faq.nucleuscms.org\/item\/79\">What does \u201cpage headers already sent\u201d mean?<\/a><\/li>\n<li>One of the more thorough explanations is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/web.archive.org\/web\/20080430141149\/http:\/\/www.expertsrt.com\/tutorials\/Matt\/HTTP_headers.html\">HTTP Headers and the PHP header() Function \u2013 A tutorial by NicholasSolutions<\/a> (Internet Archive link).<br \/>\nIt covers HTTP in detail and gives a few guidelines for rewriting scripts.<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p> <span class=\"d-none\" itemprop=\"commentCount\">20<\/span> <\/p>\n<\/div>\n<\/div>\n<p>solved How to fix \u201cHeaders already sent\u201d error in PHP <\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><\/p>\n<p><script><\/p>\n<p><\/script> <\/div>\n<p>[ad_2]<\/p>\n<h1>Solved: How to Fix &#8220;Headers Already Sent&#8221; Error in PHP<\/h1>\n<p>Have you ever encountered the dreaded &#8220;Headers already sent&#8221; error in PHP? If so, you know how frustrating it can be. Fortunately, there are a few simple steps you can take to fix this issue and get your code running smoothly again.<\/p>\n<h2>What is the &#8220;Headers Already Sent&#8221; Error?<\/h2>\n<p>The &#8220;Headers already sent&#8221; error occurs when a PHP script tries to send an HTTP header after it has already sent output to the browser. This can happen if there is whitespace or extra characters before the opening <?php tag, or after the closing ?> tag. It can also happen if you are using output buffering and the script tries to send an HTTP header after it has already sent output to the browser.<\/p>\n<h2>How to Fix the &#8220;Headers Already Sent&#8221; Error<\/h2>\n<p>The first step is to identify where the extra characters are coming from. This can be done by looking at the error message, which will tell you the exact line number where the problem is occurring. Once you have identified the source of the problem, you can take the following steps to fix it:<\/p>\n<ul>\n<li>Remove any whitespace or extra characters before the opening <?php tag.<\/li>\n<li>Remove any whitespace or extra characters after the closing ?> tag.<\/li>\n<li>If you are using output buffering, make sure that the HTTP headers are sent before any output is sent to the browser.<\/li>\n<\/ul>\n<p>Once you have taken these steps, the &#8220;Headers already sent&#8221; error should be fixed and your code should be running smoothly again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] Headers already sent error is a common issue encountered by PHP developers. This error occurs when the server sends the response to the browser before the header information is sent. This can be caused by a number of things, such as extra whitespace, incorrect line endings, or even a missing semicolon. Fortunately, this &#8230; <a title=\"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\" aria-label=\"More on (Solved) How to fix \u201cHeaders already sent\u201d error in PHP\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[423,339],"class_list":["post-946","post","type-post","status-publish","format-standard","hentry","category-solved","tag-header","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] Headers already sent error is a common issue encountered by PHP developers. This error occurs when the server sends the response to the browser before the header information is sent. This can be caused by a number of things, such as extra whitespace, incorrect line endings, or even a missing semicolon. Fortunately, this ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T16:03:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP\",\"datePublished\":\"2022-08-20T16:03:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\"},\"wordCount\":2117,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\",\"keywords\":[\"header\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\",\"name\":\"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\",\"datePublished\":\"2022-08-20T16:03:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb","og_description":"Introduction [ad_1] Headers already sent error is a common issue encountered by PHP developers. This error occurs when the server sends the response to the browser before the header information is sent. This can be caused by a number of things, such as extra whitespace, incorrect line endings, or even a missing semicolon. Fortunately, this ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T16:03:54+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP","datePublished":"2022-08-20T16:03:54+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/"},"wordCount":2117,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png","keywords":["header","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/","name":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png","datePublished":"2022-08-20T16:03:54+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/08\/Solved-How-to-fix-Headers-already-sent-error-in-PHP.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-fix-headers-already-sent-error-in-php-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) How to fix \u201cHeaders already sent\u201d error in PHP"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=946"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}