Home arrow PHP arrow Page 2 - HTTP Headers in Web Development

What do HTTP headers look like? - PHP

At this point, you may not know what HTTP headers are, but you use them all the time, both as an Internet user and as a web developer, even if you are not aware of it. The beginning of this article is a short introduction to HTTP headers, including what they are, and what they do. Later in the article, we cover some common uses of HTTP headers in web development.

TABLE OF CONTENTS:
  1. HTTP Headers in Web Development
  2. What do HTTP headers look like?
  3. How Headers Are Used
  4. Examples of HTTP Header Usage
By: John Best
Rating: starstarstarstarstar / 14
August 26, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are two different types of headers, Request headers and Response headers. Their formats differ, but generally they each consist of an opening line, header lines, and a status line. There will be an example of each later in the article.

Each of the items above must start on a new line. In response headers, the HTML or other output to the browser comes after the blank line at the end of the headers.

There may be some confusion, because the individual header directives or fields are also often referred to as "headers." I'm afraid that this article is guilty of that as well because "header directives" is too long.

Here is an example of a request header that might be sent by a web browser to a web server:

GET /index.html HTTP/1.1

Host: www.google.com
From: someone@adomain.com
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)

[blank line above]

It is pretty obvious that what this header does is request the home page of google.com from the server.

The first two lines beginning with GET and Host: are the only ones required under the HTTP/1.1 specification. GET, in the first line, is the method. Another request method you may be familiar with is POST. There are also several others.

There is a required blank line as the last line of the sample request.

Next is an example of an actual response header received from Google.com:


Date: Fri, 27 Jun 2008 00:22:19 GMT

Expires: -1

Cache-Control: private, must-revalidate, max-age=0

Content-Type: text/html; charset=UTF-8

Last-Modified: Fri, 27 Jun 2008 00:22:18 GMT

Etag: 9398183226707537952

Set-Cookie: IGTP=LI=1:LM=1214526139; expires=Sun, 27-Jun-2010 00:22:19 GMT; path=/ig;

domain=www.google.com

Content-Encoding: gzip

Server: igfe

Content-Length: 49301

X-Cache: MISS from proxy2.online.com.kh

Via: 1.0 proxy2.online.com.kh:3128 (squid/2.6.STABLE20)

Connection: keep-alive


200 OK


[HTML Content]


Let's take a look at each line of this header. The length and scope of this article doesn't permit going into detail regarding each one of these fields, but here is an overview.

Date: This just shows the date and time that the response was received.

Expires: This is the date and time that the content is considered to be out of date. This is often used for data that is updated at a fixed time, in order to prevent browsers from caching the resource beyond the date and time specified.

Cache-Control: This line contains directives regarding how the content is cached. In this case, because of must-revalidate and max-age = 0, the content is not cached.

Content-Type: This tells the browser what content type(s) to expect, and how it is encoded. It is written as a MIME type.

Last-Modified: When the content was last modified.

Etag: This is short for "entity tag." It is used for determining whether the cached URI is identical to the requested URI on the server.

Set-Cookie: Sets a cookie with expiration date and time.

Content-Encoding: How the content is encoded.

Server: The type of server sending the response.

Content-Length: In bytes.

X-Cache: This has something to do with compression.



 
 
>>> More PHP Articles          >>> More By John Best
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: