What is a PHP header? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI'm wondering about what PHP 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.
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?
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.
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 "example.com",
"example.net","example.org"
or "example.edu" 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.
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.
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).
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.
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.
One should distinguish between incomming headers (php acting as a web server page) and outgoing headers (php acting as a web client using things like the cURL library).
Headers are really just another bag of name-value pairs that goes over the wire with every HTTP "request" transaction. The Body is the ... stuff the server sends back and there is also a response-header sent back by the server to the client.
What each name-value pair in either header means is defined by the HTTP protocol and other standards but clients and servers can use headers for other uses as well.
headers() is used to set response-header Name-Value pairs while getallheaders() would be used to retrieve request headers sent to a php server page.
精彩评论