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

Here is an excerpt from a Wikipedia article:

Unlike the GET request method, where only the URL and headers sent to the server, POST requests also include the body of the message.

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

 header("Location: http://google.com"); 
+6
source share
3 answers

If you want to view the URL from your browser, enter the URL. The browser puts the URL in the HTTP REQUEST:

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

Then the web server gives you this answer:

 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" 

Each line of type "Header Header: header_value \ r \ n" is a header.
The PHP header function adds a header to the response before sending it to the user's browser.
In your example, the title:

 Location: http://google.com 

And it is added immediately after the last heading before the "empty line" (this is a line containing only \ r \ n).
POST requests are different from GET requests because you have the body of the request 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, the HTTP request is executed as follows:

  • A request / response line (POST or GET followed by the URL and the http version for the request, the Http version with the response code and the response line for the response), ending \ r \ n
  • Request / response header (header-name: header_value \ r \ n)
  • empty string (\ r \ n)
  • Response / Request Body

PS. Lines ALWAYS close with bytes "\ r \ n" ("empty lines" consist of only these two bytes).

+9
source

header() adds the header to the file.

So, if you want to set Content-Type:

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

Etc...

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

From doc :

The second special case is the "Location:" header. It not only sends this header back to the browser, but also returns the REDIRECT status code (302) to the browser if the status code 201 or 3xx is not already set.

+2
source

There are response headers and request headers

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

You set the location header in PHP as a response to the request. The browser looks at the answer and acts accordingly.

So, you go to the original page, and the location header tells the browser somewhere else.

0
source

Source: https://habr.com/ru/post/896499/


All Articles