Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of header in PHP

Tags:

php

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.

like image 561
noddy Avatar asked Feb 11 '11 17:02

noddy


People also ask

What is the use of header function?

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!

Where is header located in PHP?

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.

What is header content type in PHP?

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.

What is header location?

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.


2 Answers

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

like image 82
jerebear Avatar answered Sep 24 '22 12:09

jerebear


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:

  • https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields

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

  • http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
like image 33
Gordon Avatar answered Sep 24 '22 12:09

Gordon