What are some possible uses of the header
function in PHP?
Could someone provide me with some links for reading about this function?
Thank you.
The header() function sends a raw HTTP header to a client. It is important to notice that the header() function must be called before any actual output is sent!
Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory. replace: It is optional which indicates whether the header should add a second header or replace previous.
The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.
The HTTP Location header is a response header that is used under 2 circumstances to ask a browser to redirect a URL (status code 3xx) or provide information about the location of a newly created resource (status code of 201). Its usage is often confused with another HTTP Header which is HTTP Content-Location header.
You can do all sorts of things using the header function in PHP. Change page location, set timezone, set caching control, etc.
Here ya go http://php.net/manual/en/function.header.php
Here's a better "applicable use" tutorial as well: http://www.kirupa.com/web/header.htm
The header
function sets the headers for an HTTP Response.
From Wikipedia on HTTP Headers:
The header fields define various characteristics of the data transfer that is requested or the data that is provided in the message body. Header fields start with the field name, terminated with a colon character, followed by the field value. Field names and values may be any application-specific strings, but a core set of fields is standardized by the Internet Engineering Task Force (IETF) in RFC 2616 and other updates and extension documents (e.g., RFC 4229), and are commonly understood by all compliant protocol implementations.
There is a lengthy list of both, Request and Response parameters at that article:
What happens when a client receives an HTTP Response header depends on the individual clients. For instance, when sending
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
your browser will likely prompt you with a Login dialogue, whereas sending a
header("HTTP/1.0 404 Not Found");
could tell a Search Engine Spider that it should remove that page from it's index if it previously existed, and so on.
An important datum in Response Headers is the Status code. The Status codes are important for Clients to determine whether a previous Request could be handled by the serving server. You can find a list of Status Codes at
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With