{"id":8337,"date":"2024-01-12T21:53:07","date_gmt":"2024-01-12T18:53:07","guid":{"rendered":"https:\/\/infinitydomainhosting.com\/kb\/?p=8337"},"modified":"2025-09-24T19:10:05","modified_gmt":"2025-09-24T16:10:05","slug":"what-is-the-header-function-in-php","status":"publish","type":"post","link":"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/","title":{"rendered":"What Is the Header Function in PHP?"},"content":{"rendered":"\n<p>The <code>header<\/code> function in PHP is <a href=\"https:\/\/infinitydomainhosting.com\/kb\/virtualbox-guest-additions\/\">a<\/a> powerful tool that allows developers to send raw HTTP headers. These headers are used to control the behavior of the server and the client. This article will explain what the <code>header<\/code> function is, how it works, and when to use it. Furthermore, we will discuss some frequently asked questions related to the <code>header<\/code> function in PHP.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey 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: #999;color:#999\" 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: #999;color:#999\" 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 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#Introduction_to_the_Header_Function\" >Introduction to the Header Function<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#Syntax_of_the_Header_Function\" >Syntax of the Header Function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#How_does_the_header_function_work\" >How does the header function work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#Common_uses_of_the_header_function_in_PHP\" >Common uses of the header function in PHP<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#When_to_use_the_header_function_in_PHP\" >When to use the header function in PHP<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/infinitydomainhosting.com\/kb\/what-is-the-header-function-in-php\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Introduction_to_the_Header_Function\"><\/span>Introduction to the Header Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>header<\/code> function in PHP is used to send a raw HTTP header to the client. This allows developers to control the behavior of the server and the client by setting various parameters in the HTTP response.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Syntax_of_the_Header_Function\"><\/span>Syntax of the Header Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"code-block\">\n<p><em><strong>header(&#8216;name: value&#8217;);<\/strong><\/em><\/p>\n<\/p><\/div>\n<p>Where &#8216;name&#8217; is the name of the header and &#8216;value&#8217; is the value to be sent. Multiple headers can be sent by calling the <code>header<\/code> function multiple times.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_does_the_header_function_work\"><\/span>How does the header function work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When the <code>header<\/code> function is called in a PHP script, it sends the specified header to the client. This header is then read by the client&#8217;s browser or other HTTP client, and the corresponding behavior is controlled according to the value specified in the header.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_uses_of_the_header_function_in_PHP\"><\/span>Common uses of the header function in PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li><strong>Redirecting the user to a different page:<\/strong>\n<div class=\"code-block\">\n<p><em><strong>header(&#8216;Location: http:\/\/www.example.com&#8217;);<\/strong><\/em><\/p>\n<\/p><\/div>\n<p>This code will send a header to the client, instructing the browser to redirect to the specified URL.<\/p>\n<\/li>\n<li><strong>Setting the content type of the response:<\/strong>\n<div class=\"code-block\">\n<p><em><strong>header(&#8216;Content-Type: application\/json&#8217;);<\/strong><\/em><\/p>\n<\/p><\/div>\n<p>This code sets the content type of the HTTP response to JSON, allowing the client to understand that the response is in JSON format.<\/p>\n<\/li>\n<li><strong>Setting cookies in the client&#8217;s browser:<\/strong>\n<div class=\"code-block\">\n<p><em><strong>header(&#8216;Set-Cookie: name=value; expires=Sat, 23-May-2020 00:00:00 GMT; path=\/&#8217;);<\/strong><\/em><\/p>\n<\/p><\/div>\n<p>This code sets a cookie with the name and value specified, along with the expiration date and path.<\/p>\n<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"When_to_use_the_header_function_in_PHP\"><\/span>When to use the header function in PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It is important to use the <code>header<\/code> function in PHP only before any output is sent to the client. This is because headers must be sent before any actual content, as they control the behavior of the server and the client.<\/p>\n<p>Additionally, it is important to use the <code>header<\/code> function carefully and with caution. Sending incorrect or conflicting headers can lead to unexpected behavior and errors in the client&#8217;s browser.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>1. What happens if I use the header function after output has already been sent?<\/strong><\/p>\n<p><!--KB_CAT_BLOCK--><\/p>\n<figure class=\"kb-cat-placeholder\" style=\"margin:1.75rem 0;display:block;\"><img src=\"https:\/\/infinitydomainhosting.com\/kb\/assets\/img\/cat-php-scripts.webp\" alt=\"What Is the Header Function in PHP?\" loading=\"lazy\" decoding=\"async\" style=\"max-width:100%;height:auto;display:block;border-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,0.12);\" \/><\/figure>\n<p>If the <code>header<\/code> function is called after output has already been sent to the client, a warning will be generated by PHP. In some cases, this may not be visible if error reporting is turned off. However, it is important to avoid this situation as it can lead to unexpected behavior in the client&#8217;s browser.<\/p>\n<p><strong>2. Can I send multiple headers at once using the header function?<\/strong><\/p>\n<p>Yes, multiple headers can be sent to the client by calling the <code>header<\/code> function multiple times. However, it is important to ensure that the headers are sent in the correct order and that they do not conflict with each other.<\/p>\n<p><strong>3. Is it possible to remove a previously set header using the header function?<\/strong><\/p>\n<p>No, it is not possible to remove a previously set header using the <code>header<\/code> function. Once a header has been sent to the client, it cannot be changed or removed. However, it is possible to override a previously set header by sending a new header with the same name.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In conclusion, the <a href=\"https:\/\/www.simplilearn.com\/tutorials\/php-tutorial\/header-in-php\" target=\"_blank\" rel=\"noopener\">header function in PHP<\/a> is a powerful tool that allows developers to control the behavior of the server and the client by sending raw HTTP headers. It is important to use the <code>header<\/code> function carefully and with caution, as sending incorrect or conflicting headers can lead to unexpected behavior and errors in the client&#8217;s browser. By understanding the <code>header<\/code> function and its common uses, developers can create more dynamic and interactive web applications.<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The header function in PHP is a powerful tool that allows developers to send raw HTTP headers. These headers are used to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":43307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[3],"tags":[1232,70],"class_list":["post-8337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-scripts","tag-header-function","tag-php"],"_links":{"self":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/8337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/comments?post=8337"}],"version-history":[{"count":4,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/8337\/revisions"}],"predecessor-version":[{"id":43308,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/posts\/8337\/revisions\/43308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/media\/43307"}],"wp:attachment":[{"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/media?parent=8337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/categories?post=8337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infinitydomainhosting.com\/kb\/wp-json\/wp\/v2\/tags?post=8337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}