{"id":619,"date":"2021-07-05T08:59:15","date_gmt":"2021-07-05T13:59:15","guid":{"rendered":"https:\/\/www.knownhost.com\/kb\/?p=619"},"modified":"2021-10-18T06:47:12","modified_gmt":"2021-10-18T11:47:12","slug":"what-is-the-headers-already-sent-php-error","status":"publish","type":"post","link":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/","title":{"rendered":"What is the &#8220;headers already sent&#8221; PHP error?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #212121;color:#212121\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #212121;color:#212121\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#why_does_it_happen\" >Why does it happen?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#functions_that_modify_the_http_header\" >Functions that modify the HTTP header<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#possible_solution\" >Possible solution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#referenced_and_helpful_resources\" >Referenced and helpful resources<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>A common error seen, however rarely known or understood is the &#8220;headers already sent&#8221; error. Which may look similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  Warning: Cannot modify header information - headers already sent by (output started at \/some\/file.php:12) in \/some\/file.php on line 23<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"why_does_it_happen\"><\/span>Why does it happen?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To understand why headers must be sent before output it&#8217;s necessary to look at a typical HTTP response. PHP scripts mainly generate&nbsp;HTML&nbsp;content, but also pass a set of HTTP\/CGI headers to the webserver:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  HTTP\/1.1200 OK\n  Powered-By: PHP\/5.3.7Vary:Accept-EncodingContent-Type: text\/html; charset=utf-8&lt;html&gt;&lt;head&gt;&lt;title&gt;PHP page output page&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;Content&lt;\/h1&gt; &lt;p&gt;Some more output follows...&lt;\/p&gt;and&lt;a href=\"\/\"&gt;&lt;img src=about:note&gt;...<\/code><\/pre>\n\n\n\n<p>The page\/output always follows the headers. PHP is required to pass the headers to the web server first. It can only do that once. And after the double linebreak, it can&#8217;t ever append to them again.<\/p>\n\n\n\n<p>When PHP receives the first output (<code>print<\/code>,&nbsp;<code>echo<\/code>,&nbsp;<code>&lt;html&gt;<\/code>) it will &#8220;flush&#8221; the collected headers. Afterward, it can send all the output bits it wants. But sending further headers is impossible from then.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"functions_that_modify_the_http_header\"><\/span>Functions that modify the HTTP header<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>header<\/li><li>header_remove<\/li><li>session_start<\/li><li>session_regenerate_id<\/li><li>setcookie<\/li><li>setrawcookie<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"possible_solution\"><\/span>Possible solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"kb-shortcode kb-shortcode_info\">\n                    <div class=\"kb-shortcode-icon\">\n                        <i class=\"fa fa-info-circle fa-2x\"><\/i>\n                    <\/div>\n                    <div class=\"kb-shortcode-content_info\">As all developmental issues are different, and coded in different ways, this is only a suggestion of a possible solution.<\/div>\n                <\/div>\n\n\n\n<p>Just because you cannot see anything does not mean that PHP sees the same.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Download the file mentioned in the <a href=\"https:\/\/www.knownhost.com\/kb\/enable-php-error-logging-for-debug-testing\/\">error message<\/a> via&nbsp;FTP&nbsp;or the file manager provided in your control panel.<\/li><li>Open the file in a plain text editor (notepad or similar is the recommended).<\/li><li>Ensure that the first characters are<code> &lt;?php<\/code><\/li><li>Ensure that the last characters are either NOT a PHP closing tag or a closing tag <code>?&gt;<\/code> with no blank lines or spaces after it.<\/li><li>Before saving, or use the Save as dialog, ensure the file encoding is anything without the &#8216;BOM&#8217; suffix.<\/li><\/ol>\n\n\n\n<p>To be sure about the end of the file, do this:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Place the cursor between the ? and &gt;<\/li><li>Now press the DELETE key on your computer<ul><li>Note to MAC users: The &#8220;DELETE&#8221; key on a PC deletes characters to the right of the cursor. That is the key noted here.<\/li><\/ul><\/li><li>Keep that key pressed<\/li><li>For at least 15 seconds<\/li><li>Now type &gt; and<\/li><li>save without pressing any other key at all.<\/li><li>If you press another key, you will bring the problem back.<\/li><li>DO NOT PUT CODE IN UNNECESSARY CODE BLOCKS, PUT THEM IN A SINGLE PHP BLOCK.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"referenced_and_helpful_resources\"><\/span>Referenced and helpful resources<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/stackoverflow.com\/questions\/8028957\/how-to-fix-headers-already-sent-error-in-php\">stackoverflow.com\/questions\/8028957\/how-to-fix-headers-already-sent-error-in-php<\/a><\/li><li><a href=\"http:\/\/php.net\/manual\/en\/function.headers-sent.php\">php.net\/manual\/en\/function.headers-sent.php<\/a><\/li><li><a href=\"http:\/\/codex.wordpress.org\/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F\">codex.wordpress.org\/FAQ_Troubleshooting#How_do_I_solve_the_Headers_already_sent_warning_problem.3F<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A common error seen, however rarely known or understood is the &#8220;headers already sent&#8221; error. Which may look similar to this: Why does it happen? To understand why headers must be sent before output it&#8217;s necessary to look at a typical HTTP response. PHP scripts mainly generate&nbsp;HTML&nbsp;content, but also pass a set of HTTP\/CGI headers [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[155,308,198],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-common-issues","tag-debug","tag-headers","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is the &quot;headers already sent&quot; PHP error? - KnownHost<\/title>\n<meta name=\"description\" content=\"Learn more about what &quot;Headers already sent&quot; error are, and how to solve it on your KnownHost hosted website.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is the &quot;headers already sent&quot; PHP error? - KnownHost\" \/>\n<meta property=\"og:description\" content=\"Learn more about what &quot;Headers already sent&quot; error are, and how to solve it on your KnownHost hosted website.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/\" \/>\n<meta property=\"og:site_name\" content=\"KnownHost\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-05T13:59:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-18T11:47:12+00:00\" \/>\n<meta name=\"author\" content=\"Jonathan K. W.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jonathan K. W.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/\"},\"author\":{\"name\":\"Jonathan K. W.\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/#\\\/schema\\\/person\\\/3db6e20d1f33519cd68fe0ba1230a48b\"},\"headline\":\"What is the &#8220;headers already sent&#8221; PHP error?\",\"datePublished\":\"2021-07-05T13:59:15+00:00\",\"dateModified\":\"2021-10-18T11:47:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/\"},\"wordCount\":406,\"keywords\":[\"debug\",\"headers\",\"php\"],\"articleSection\":[\"Common Issues\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/\",\"url\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/\",\"name\":\"What is the \\\"headers already sent\\\" PHP error? - KnownHost\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/#website\"},\"datePublished\":\"2021-07-05T13:59:15+00:00\",\"dateModified\":\"2021-10-18T11:47:12+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/#\\\/schema\\\/person\\\/3db6e20d1f33519cd68fe0ba1230a48b\"},\"description\":\"Learn more about what \\\"Headers already sent\\\" error are, and how to solve it on your KnownHost hosted website.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/what-is-the-headers-already-sent-php-error\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is the &#8220;headers already sent&#8221; PHP error?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/#website\",\"url\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/\",\"name\":\"KnownHost\",\"description\":\"KnownHost provides a comprehensive webhosting knowledge base to help answer many of your common webhosting and linux questions.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.knownhost.com\\\/kb\\\/#\\\/schema\\\/person\\\/3db6e20d1f33519cd68fe0ba1230a48b\",\"name\":\"Jonathan K. W.\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g\",\"caption\":\"Jonathan K. W.\"},\"sameAs\":[\"https:\\\/\\\/www.knownhost.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is the \"headers already sent\" PHP error? - KnownHost","description":"Learn more about what \"Headers already sent\" error are, and how to solve it on your KnownHost hosted website.","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:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/","og_locale":"en_US","og_type":"article","og_title":"What is the \"headers already sent\" PHP error? - KnownHost","og_description":"Learn more about what \"Headers already sent\" error are, and how to solve it on your KnownHost hosted website.","og_url":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/","og_site_name":"KnownHost","article_published_time":"2021-07-05T13:59:15+00:00","article_modified_time":"2021-10-18T11:47:12+00:00","author":"Jonathan K. W.","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jonathan K. W.","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#article","isPartOf":{"@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/"},"author":{"name":"Jonathan K. W.","@id":"https:\/\/www.knownhost.com\/kb\/#\/schema\/person\/3db6e20d1f33519cd68fe0ba1230a48b"},"headline":"What is the &#8220;headers already sent&#8221; PHP error?","datePublished":"2021-07-05T13:59:15+00:00","dateModified":"2021-10-18T11:47:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/"},"wordCount":406,"keywords":["debug","headers","php"],"articleSection":["Common Issues"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/","url":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/","name":"What is the \"headers already sent\" PHP error? - KnownHost","isPartOf":{"@id":"https:\/\/www.knownhost.com\/kb\/#website"},"datePublished":"2021-07-05T13:59:15+00:00","dateModified":"2021-10-18T11:47:12+00:00","author":{"@id":"https:\/\/www.knownhost.com\/kb\/#\/schema\/person\/3db6e20d1f33519cd68fe0ba1230a48b"},"description":"Learn more about what \"Headers already sent\" error are, and how to solve it on your KnownHost hosted website.","breadcrumb":{"@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.knownhost.com\/kb\/what-is-the-headers-already-sent-php-error\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.knownhost.com\/kb\/"},{"@type":"ListItem","position":2,"name":"What is the &#8220;headers already sent&#8221; PHP error?"}]},{"@type":"WebSite","@id":"https:\/\/www.knownhost.com\/kb\/#website","url":"https:\/\/www.knownhost.com\/kb\/","name":"KnownHost","description":"KnownHost provides a comprehensive webhosting knowledge base to help answer many of your common webhosting and linux questions.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.knownhost.com\/kb\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.knownhost.com\/kb\/#\/schema\/person\/3db6e20d1f33519cd68fe0ba1230a48b","name":"Jonathan K. W.","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f432b99e6651fe8d1deb57a285bd84e806f1c9ae8b4c6c585d7e3a0b33789ad9?s=96&d=mm&r=g","caption":"Jonathan K. W."},"sameAs":["https:\/\/www.knownhost.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":0,"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knownhost.com\/kb\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}