Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the HTTP request URL not part of the HTTP request header?

Tags:

http

php

Here's an excerpt from a Wikipedia article:

In contrast to the GET request method where only a URL and headers are sent to the server, POST requests also include a message body.

Based on that, it looks like the URL is sent separately from the header, but if that's so, why do we use the header() method in PHP to set a URL to redirect to?

header("Location: http://google.com");
like image 468
Emanuil Rusev Avatar asked Sep 02 '11 14:09

Emanuil Rusev


People also ask

Does HTTP request contain URL?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.

What is request URL in header?

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.

Which part of URL is header?

A website header sits at the top of each page and serves a few very important purposes. This does more than provide a place for your logo; it is part of a consistent user experience that all good websites share.

What is included in request header?

Request headers contain more information about the resource to be fetched, or about the client requesting the resource. Response headers hold additional information about the response, like its location or about the server providing it.


2 Answers

When you want to browse an URL from your browser you type an URL. The browser puts the url inside an HTTP REQUEST like this:

GET /path/to/resource.php?var=data1&othervar=data2 HTTP/1.1
Host: example.com
Connection: keep-alive
"empty line"

Then a webserver gives you an answer like this:

HTTP/1.0 200 OK
Date: Fri, 02 Sep 2011 14:37:36 GMT
Server: Apache
Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 149
Content-Type: text/javascript; charset=utf-8
Connection: keep-alive
"empty line"
"149 bytes of Response data"

Every line like this "Header-Name: header_value\r\n" is an header.
PHP header function adds an header to the response before sending it to user's browser.
In your example the header is:

Location: http://google.com

And it's added just after the last header before the "empty line" (which is a line which contains only a \r\n).
POST requests are different from GET requests because you have a request body after the "empty line"):

POST /path/to/resource.php HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: "number of bytes in the body"
"empty line"
variable=data&othervar=data2

In conclusion an HTTP request is made like this:

  1. Request/response row (POST or GET followed by url and http version for request, Http version followed by response code and response string for the response) ended with \r\n
  2. Request/response headers (header-name: header_value\r\n)
  3. empty row (\r\n)
  4. Response/request body

PS. Rows are ALWAYS closed by "\r\n" bytes ("empty lines" are made of just those two bytes).

like image 77
CaNNaDaRk Avatar answered Sep 28 '22 21:09

CaNNaDaRk


header() adds a header to the file.

So if you want to set the Content-Type:

header("Content-type: text/javascript");

And so on...

Location is just another header you can set and/or change with the php header() function

From the doc:

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

like image 30
Naftali Avatar answered Sep 28 '22 21:09

Naftali