tags:

views:

239

answers:

7

I'm wondering about what headers are. I use PHP strictly for HTML completion and I thought I had to send text/html header when the output was text and then image/jpeg header from a separate script which was used as source in an image tag but then someone suggested me to take away the headers because they made nothing... And I did and everything was the same as before... This made me wonder, what are headers, when are they used, both from an HTML perspective but also from some (?) other perspective and why could I remove mine?

THANKS EVERYONE, I'M ENLIGHTENED :)

+1  A: 

HTTP request/response headers (in your case response), are meta-data which are sent together with the response (more precisely, before the data).

It may contain information such as caching directives, type and size of the data, date of last modification, etc. You can see a list of HTTP headers here.

Artefacto
+2  A: 

assuming you mean the header() function.

It is a way of adding/changing HTTP headers that are sent to the browser.

You might need to lookup a list of these headers in the HTTP Specification.

An example, you could be developing an XML RSS Feed, so you would need change the Content-Type to "text/xml" instead of "text/html" (Default).

Ollie
Thanks for the clarification about text/html being a default.
fast-reflexes
+2  A: 

Headers aren't a php thing, it is a HTTP thing (a way to transmit data over the internet).

The header() function in php allows you to set these headers to correspond to the use of your code, so if your code generates a jpg and not, as normally, a text (like html or javascript which is then interpreted by the user's browser) you set the coressponding header to image/jpeg. Now you can use this script elsewhere as if it where a static image, like in:

<img src="static.jpg"/>

Now, if you want to generate a thumbnail of an image, and want to access this dynamically, you write a php script which returns a jpeg, and you can use it like this:

<img src="thumb.php?s=64&name=static.jpg"/>"

The thumb.php document would generate a 64x64 thumb from the image static.jpg (using the HTTP header Content-Type: image/jpeg.

Of course, this is just one of the many usages of the header() function, for aditional information, have a look in wikipedia: Hypertext Transfer Protocol, List of HTTP header fields, or if you are brave enough, in the rfc.

Femaref
Ok, so you like you said "normally" PHP input is considered text to a browser? text/html specifically or am I wrong? If I just send some PHP to the browser, how will it interpret it? Also, in the thumb script above.. would I have to use an image/jpeg header there or not? Because in my case it turns out well without an image/jpeg header and that makes me wonder...
fast-reflexes
A browser would just print the php code verbatim, as php is a serverside language, the browser can't interpret it. A php script usually puts out a text of some sort (html), which can be interpreted by the browser. With your case, maybe your browser realized that the source is binary data/an image and displayed it, but it's possible not all browsers have this behaviour.
Femaref
Yeah ok.. I see... I mean, how will my server send it to the browser if I don't supply a header? Is it like html? Also, regaridng the behavious..is is the case, that since the thumb.php is supplied as source in an IMG tag, the browser expects it to come an image and therefore, the image/jpeg header is not a must?
fast-reflexes
It depends on the browser. Some will try to detect the mime-type of the incoming data in order to determine what to do with it. Others will trust the extension, e.g. .jpg for JPEG image data. The different ways of handling between IE and Firefox can actually lead to some annoying issues when developers don't test properly.
Martijn Heemels
Ok... so therefore I should always provide headers? What I'm missing completely here is the following. If I have a page called index.php, and I access it with my browser, what header would be in use if no header was supplied from the php script? Does browsers try to solve this, case by case by inspecting the incoming data, or is there a default that the browser will consider anything that is accessed via an URI if no other header is specified?
fast-reflexes
+2  A: 

headers have instructions for the browser to tell it to render content a certain way. For instance, specifying the page language / char set to use, or telling the browser the data fixing to be output is raw image data so process it as an image (as opposed to regular text) or you can do like header("Location: somepage.html") to have the browser redirect to another page. Basically a lot of the stuff (and then some) that you would find in your normal <head>...</head> tags.

Crayon Violent
Ok so.. 1. Headers is something specific to browsers? 2. 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?
fast-reflexes
@fast-reflexes headers are not about browsers specifically, they are part of HTTP - the protocol. You know, as in http://example.com - it's different than ftp://example.com, in that the content is retrieved using the HTTP protocol. It doesn't matter if you're using a browser, another program, something like cURL, etc. It actually has nothing to do with PHP other than how the use of PHP is associated with web servers. You can view the default headers your webserver is sending with Firebug or Chrome/Safari's Inpsector.
Alex JL
headers are sent to all browsers. Most browsers try to use "default" settings if none are specified (for things like language, char set, etc...)
Crayon Violent
Ok so there are several different type of headers and it's part of the HTTP protocol, I see. This means I can send several headers from PHP before I start the actual output, correct? But only one Content header per output, right?
fast-reflexes
@fast-reflexes yes, a typical page view on a site involves 4-5 Request Headers and 7-8 Response Headers. So yes, you can send as many as you need. But, I don't suggest doing this until you understand what they are and what each one does. And yes, it does not make sense at all to send more than one Content-Type header for a single response. It's like saying 'This file is html text, a jpeg image, AND an adobe flash video!'.
Alex JL
+22  A: 

Well you see, when daddy loves mummy very much, he wants to prove his love by using its big Web server from which come plenty of HTML tags that goes inside mummy's browser.

But to do that, he must inform mummy of what he's planning to do, otherwise mummy could be very surprised by daddy's present.

Therefor, daddy starts slowly with mummy, explaining to her which kind of data he is going to send (mime/type), which language he is going to speak (charset), or which protection he is going to use (status code).

And the PHP header is some toy to help you doing that with an elephant. Err...

e-satis
Ha, surely one can explain something other than THAT to a five year old?
Alex JL
This is what I call answering the question. Also, I love the ElePHPant reference.
BoltClock
HTTP header are something all 5 years all should know about, or how would they be prepared to hit on hot fake accounts on facebook ?
e-satis
Hah, made my day!
alexn
+ 1 Awesome, hahah ! Just don't ever .. EVER explain to a 5 year old how to use an USB stick ...
FreekOne
But what about dual boot ?
e-satis
@e-satis: regarding that, allow me to quote a great man: "Giggidy !"
FreekOne
+7  A: 

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"&gt;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.

Lekensteyn
+1 for netcat demonstration.
Rushyo
I understand, thanks... Please clarify the sentence with "The Server is ignored by..." I didn't quite get that. Also, this means I can send several different headers per output but not several of the same kind, like Content-Type for example... I have to specify ONE Content-Type per out put otherwise the browser won't know what I expect from him, correct?
fast-reflexes
"The `Server` is" should be "The `Server` header [field]`". Multiple `Content-Type` fields are not valid, see the modified answer.
Lekensteyn
+3  A: 

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.

bobince
`header('Content-Type', 'image/jpeg')` will cause an error. The correct syntax is `header('Content-Type: image/jpeg')`
Chris T
Thanks, this was a good explanation. So basically, it's always safer to include headers if they're anything else than text/html, even though most browsers may get it right anyways in some cases, one of which is the earlier mentioned case with the image?
fast-reflexes
@Chris: thanks, I came over a bit Python there. @fast: yes, that's true of the `Content-Type` header. Other headers have different uses. (`Content-Disposition: attachment` to force download, `Location` to redirect, etc.)
bobince