HTTP Headers In PHP
Sessions
- Introduction to PHP
- Form Handling in PHP
- Using Variables and Expressions in PHP
- Using Variables and Expressions in PHP (Lab)
- PHP Operators
- PHP Operators (Lab)
- Conditional Statements in PHP
- Conditional Statements in PHP (Lab)
- Flow Control in PHP
- Flow Control in PHP (Lab)
- Functions in PHP
- Functions in PHP (Lab)
- Working with Arrays
- Working with Arrays (Lab)
- Handling Databases with PHP
- Working with Cookies
- Working with Cookies (Lab)
- Session Management in PHP
- Handling E-mail with PHP
- Handling E-mail with PHP (Lab)
- OOP Concepts
- OOP Concepts (Lab)
HTTP Headers
HTTP is a network transmission protocol that transfers hypertext files. It provides instructions
for communication between the client and the server. HTTP runs on the Transmission Control
Protocol/Internet Protocol (TCP/IP) suite, which is the foundation protocol suite for the Internet.
###Structure of an HTTP Message
An HTTP header is an Internet protocol containing instructions to transfer information between a Web
client and a Web server. The instruction can either be a request sent by the client to the server for some
resource or a response from the server to a client request. There are two types of headers namely,
request headers and response headers.
The format for request and response headers is similar and contains the following structure or
components:
A request or a response line
HTTP header line
A blank line
A message body, which is optional
The format of an HTTP message is as follows:
<initial line, different for request vs. response>
Header1: value1
Header2: value2
Header3: value3
Blank line
<optional message body, like file contents or query data>
###Initial Request or Response Line
The first part of an HTTP header, which is a request or a response line, differs in format.
A request line contains the following information separated by spaces:
An HTTP method name
The address or path of the requested resource. It is also called the Uniform Resource Identifier
(URI)
The HTTP version being used
Code Snippet displays an initial line with a request message
Code Snippet
GET /sample.html HTTP/1.1
The code uses the HTTP method, GET, to request for a sample.html file for an HTTP client of version
1.1. The HTTP version is always specified in 'HTTP/x.x' upper case format.
A response line consists of the following three components separated by spaces
The HTTP version
A response code indicating the result of the request
An English phrase describing the response code
Code Snippet displays an initial line with a response message.
HTTP/1.0 500 Internal Server Error
where,
HTTP/1.0 - specifies the HTTP version
500 - specifies the response code
Internal Server Error - specifies the description of the response code
###Header Lines
The header lines provide information about the request or response or the data sent in the message
body.
The syntax for a header line is as follows:
Syntax:
Header-Name: value
HTTP headers are classified into the following categories:
General
Is used to control the processing of a message and provide extra information to the
receiver. They are not specific to any request or response message.
Entity
Provides information about the entity, if present in the body of a request.
Request or response
Provides the server with details about the client's request and enables the
client to have control on the processing of requests. On receiving the request, the server returns
the response header attached with the response being sent. The headers are specific for request
or response messages.
Code Snippet displays HTTP header lines.
Code Snippet
GET /sample.html HTTP/1.1
User-agent: Mozilla/4.0
Last-Modified: Mon, 11 Apr 2011 23:07:07 GMT
Accept-Language: en
[ blank line above ]
where,
GET
is the initial request line that specifies GET as the method, the requested file name and the
version of HTTP used
User-agent
is a header line that specifies the name of the browser and the version
Last-Modified
is a header line that specifies the date and time when the resource was last
modified
Accept-Language
is a header line that specifies the language preference as English6.4
The Message Body
The third and an optional component of an HTTP header is the message body that appears after the
header lines. In a response message, the requested resource is returned to the client in the message
body. In a request message, the message body will contain user data and uploaded files that are sent to
the server.
Using HTTP Headers in PHP
In PHP, the header() function is used to generate the HTTP headers. The header() function sends
the HTTP commands to the server through HTTP protocols. After the execution of header() function, it
displays a blank line showing that the header information is complete.
To use a header() function, the syntax is as follows:
Syntax:
void header( string string [,bool replace [,int http_response_code]] )
where,
string -
is a required parameter and specifies the header string to be sent
replace -
is an optional parameter. Indicates whether the header should replace the previous or
add a second header.
http_response_code
- is an optional parameter. Forces the HTTP response code to the specified
value
Code Snippet displays the use of an authentication header
Code Snippet
<?php
header('WWW-Authenticate: Negotiate');
?>
Authentication helps to identify if a client is allowed to access to a resource. It is a means of negotiating
access to a secure resource.
The initial request from a client will not contain any authentication information. An HTTP server application
can deny the request indicating that authentication is required. The server application then sends
WWW-Authentication headers with the supported authentication schemes. The commonly used
authentication schemes are as follows:
Http Basic Authentication:
sends an encoded string that contains a user name and password
for the client
HTTP Digest Authentication:
is a challenge-response scheme. The server sends a data string
to the client as a challenge. The client responds with a user name and password, among other
additional information.
NTLM (defined by Microsoft):
is a challenge-response scheme that uses Windows credentials to
transform the challenge data instead of sending the unencoded user name and password details.
This scheme requires multiple exchanges between the client and server.
Negotiate (defined by Microsoft):
has the following protocols:
Kerberos
NT LAN Manager (NTLM)
The Negotiate scheme selects between Kerberos and NTLM depending on their availability.
Posted on Utopian.io - Rewarding Open Source Contributors
Continuous plagiarized submissions and disregard to our prior comments has led to your full ban from utilizing utopian platform.
Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.
[utopian-moderator]
Hey @mcfarhat, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Congratulations @arshi731! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP