Is there a DEFAULT header? If I just send some PHP to a browser without specifying a header.. what header will it apply to it?

There are lots of different HTTP headers that mean different things. PHP will give you defaults for the important ones if you don't set them yourself.

I think the header you're specifically talking about is Content-Type. This tells the browser what kind of file you're sending it. If you say text/html, it will try to display what you give it as a web page. If you say application/pdf, it'll try to display or download it as a PDF file.

PHP defaults to sending Content-Type: text/html. If that's all you want, you don't have to call header('Content-Type: ...'); at all. However, if you are using any non-ASCII Unicode characters, you may wish to set Content-Type to text/html;charset=something, where something is the encoding you're using for them (often, utf-8). Otherwise the browser will have to guess and might get it wrong. The commonly-seen <meta http-equiv="Content-Type" content="text/html;charset=something"/> tag is an alternative way of doing the same thing; if you want to be really safe about it, you can use both.

If you serve a JPEG image as text/html, which is what will be happening if you follow “someone”'s questionable advice by removing the header() call, then going to the URL of the image in the browser will try to display the binary image as HTML, which will give you a big old load of garbage on the screen. That's not very good, really.

However in many browsers, such a broken JPEG will still usually work when you point an <img src> tag at it. This is because when you use an <img>, the browser knows it's going to be fetching an image, and ignores you when you say it's actually HTML. It then has to to ‘sniff’ the contents of the file to see whether it looks like a JPEG, a GIF, a PNG, or some other kind of image it knows about, so it knows how to display it. Browsers have done this because there are so many poorly-written sites out there that forget to send the header. Boo!

So definitely send header('Content-Type: image/jpeg') when you're writing a JPEG, or any other non-HTML type. For HTML pages, you can often get away without it.

Answer from bobince on Stack Overflow
🌐
PHP
php.net › manual › en › function.header.php
PHP: header - Manual
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output ...
🌐
W3Schools
w3schools.com › php › func_network_header.asp
PHP header() Function
Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box): <?php header("Content-type:application/pdf"); // It will be called downloaded.pdf header("Content-Disposition:attachment;filename='downloaded.pdf'"); // The PDF source is in original.pdf readfile("original.pdf"); ?> <html> <body> ...
Discussions

html - How to set Headers correctly in PHP - Stack Overflow
Don't set the header before any echo and print statement. ... @EdCottrell, yea... I am super noob. I am just so confused. I spent so long trying to figure this out... I'm trying to learn this on my own and I would like some guidance... ... @Deepak saini, I thought you were supposed to? All PHP ... More on stackoverflow.com
🌐 stackoverflow.com
Why would some programmers use include'header.php' instead of just making it as a function header();
Different philosophies. Most people wouldn't do either. More on reddit.com
🌐 r/PHPhelp
43
3
December 10, 2023
http redirect - PHP header(Location: ...): Force URL change in address bar - Stack Overflow
I'm currently working on a mobile site with authentication using PHP sessions with a database. I have a login page with a form that goes to server_login.php on submit. The php file then creates some More on stackoverflow.com
🌐 stackoverflow.com
PHP header(location) function not working
Jimmy Seeber is having issues with: I'm working on the "Building a simple PHP application" deep dive and am stuck on the header function not redirecting my user to the 'Th... More on teamtreehouse.com
🌐 teamtreehouse.com
16
April 12, 2013
Top answer
1 of 7
29

Is there a DEFAULT header? If I just send some PHP to a browser without specifying a header.. what header will it apply to it?

There are lots of different HTTP headers that mean different things. PHP will give you defaults for the important ones if you don't set them yourself.

I think the header you're specifically talking about is Content-Type. This tells the browser what kind of file you're sending it. If you say text/html, it will try to display what you give it as a web page. If you say application/pdf, it'll try to display or download it as a PDF file.

PHP defaults to sending Content-Type: text/html. If that's all you want, you don't have to call header('Content-Type: ...'); at all. However, if you are using any non-ASCII Unicode characters, you may wish to set Content-Type to text/html;charset=something, where something is the encoding you're using for them (often, utf-8). Otherwise the browser will have to guess and might get it wrong. The commonly-seen <meta http-equiv="Content-Type" content="text/html;charset=something"/> tag is an alternative way of doing the same thing; if you want to be really safe about it, you can use both.

If you serve a JPEG image as text/html, which is what will be happening if you follow “someone”'s questionable advice by removing the header() call, then going to the URL of the image in the browser will try to display the binary image as HTML, which will give you a big old load of garbage on the screen. That's not very good, really.

However in many browsers, such a broken JPEG will still usually work when you point an <img src> tag at it. This is because when you use an <img>, the browser knows it's going to be fetching an image, and ignores you when you say it's actually HTML. It then has to to ‘sniff’ the contents of the file to see whether it looks like a JPEG, a GIF, a PNG, or some other kind of image it knows about, so it knows how to display it. Browsers have done this because there are so many poorly-written sites out there that forget to send the header. Boo!

So definitely send header('Content-Type: image/jpeg') when you're writing a JPEG, or any other non-HTML type. For HTML pages, you can often get away without it.

2 of 7
11

Headers are not specific to browsers, it's a part of the HTTP protocol.
A request for a page (or any other resource like images) will cause the client (e.g. Internet browser) to send a request header. This could contain an header for language (Accept-Language) for example.

The first line of a HTTP request is in the format METHOD RESOURCE HTTP/VERSION. Example: GET /resource HTTP/1.0.
HTTP/1.1 requires the Host-header. An example HTTP/1.1 request:

GET / HTTP/1.1
Host: example.com

The server responds with at least a status code: HTTP/1.1 200 OK
Most servers will send additional headers. Common headers are: Content-Type, Date, Server and Content-Length.

This is an example request (raw data):

$ nc example.com 80
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Sat, 11 Sep 2010 19:12:13 GMT
Server: Apache
Last-Modified: Fri, 30 Jul 2010 15:30:18 GMT
ETag: "573c1-254-48c9c87349680"
Accept-Ranges: bytes
Content-Length: 596
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <TITLE>Example Web Page</TITLE>
</HEAD> 
<body>  
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,&quot;example.org&quot
  or &quot;example.edu&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not available 
  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 
  2606</a>, Section 3.</p>
</BODY>
</HTML>

It's up to the client (Internet browser) whether to parse a header or not. All modern Internet browsers parses the Content-Type header, and use it to determine how to display a resource (is it a HTML page, an image, a text file or something else?). The Server header is ignored by browsers, servers use it to identify themselves. But some crawler might use it for statistics.

A quote from the HTTP specification:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.

That means that multiple Content-Type fields are not valid, and the behaviour is undefined (although it's common to use the last defined one).

This Wikipedia article contains a list of headers with a description.

🌐
GeeksforGeeks
geeksforgeeks.org › php › php-header-function
PHP | header() Function - GeeksforGeeks
July 11, 2025 - The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent.
🌐
GitHub
gist.github.com › irazasyed › 5788118
PHP HTTP Headers (Examples) · GitHub
PHP HTTP Headers (Examples). GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Envato Tuts+
code.tutsplus.com › home › wordpress › theme development
The header.php File: What Needs to Go in It and What Doesn't | Envato Tuts+
September 22, 2022 - Remember that the header of your site is the content which is shown on all the pages of your site. For example, the logo and menu are shown on all the pages of your site, and thus, they should be included in the header.php file.
🌐
Reddit
reddit.com › r/phphelp › why would some programmers use include'header.php' instead of just making it as a function header();
r/PHPhelp on Reddit: Why would some programmers use include'header.php' instead of just making it as a function header();
December 10, 2023 - In the archival projects I work with the previous developer created entire pages by placing html concatenated with php vars and functions into strings and concatenating those strings to other strings and then echoing the final string, it was an incredible mess and pretty unmanageable. I often create a partials folder, under that I put the well formatted html chunks into include files in proper folders, headers, footers, tables, table rows, etc., it allows me to see at a glance that my tags are ended properly, etc.
🌐
Team Treehouse
teamtreehouse.com › community › php-include-headerfooter-best-practice
PHP include header/footer best practice (Example) | Treehouse Community
June 20, 2014 - Hopefully someone more advanced with php can chime in on this one. ... I'd say it really depends on what the site is that you're trying to code... ... Method 1 saves time at the outset of a project when you're building a template. However, later in development, when it is time to begin working on canonical tags or some such optimization that can be done in the header, these optimizations might need to be page-specific.
🌐
Edureka
edureka.co › blog › header-location-in-php
Header Location In PHP | PHP Header Location Edureka
February 25, 2025 - This article on Header Location In PHP will introduce to header function in PHP along with the practical demonstration for the same.
🌐
WordPress
developer.wordpress.org › reference › functions › get_header
get_header() – Function | Developer.WordPress.org
The file names for the home and 404 headers should be header-home.php and header-404.php respectively.
🌐
Ink Plant
inkplant.com › tools › content-type-headers
PHP Content-Type Headers
<?php header('Content-Type: text/plain; charset=utf-8'); ?>
🌐
Udemy
blog.udemy.com › home › php header: xml header coding tutorial
PHP Header: XML Header Coding Tutorial - Udemy Blog
December 4, 2019 - While the header file defines the interface of the functions and subroutines with the rest of the code, the implementation defines the function of the code. However, PHP lacks this divided file architecture.
🌐
InfinityFree
forum.infinityfree.com › hosting support
Using header in php - Hosting Support - InfinityFree Forum
February 9, 2021 - What exactly were you putting as the header? I know from experience that it works perfectly fine · Well I was making a login page using files and the code looked something like this if (usernameexists() === true) { header(“Location: usernameused.html”); } · The forum supports Markdown, ...
🌐
SitePoint
sitepoint.com › php
Using header to move between pages - PHP - SitePoint Forums | Web Development & Design Community
April 2, 2017 - I have been using header location to move between my php pages so that the url displayed in the address bar does not change. When doing so, I get the following error Warning: session_start(): Cannot send session cache limiter - headers already sent This is what I am using to get me to the account ...