Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)

Until now my php application assumed HTTP 1.1 everywhere. So I defined all headers like so:

header("HTTP/1.1 500 Internal Server Error"); 

But now my server also supports HTTP 2 and I want to update all header responses with the right HTTP status code.

How to I get the HTTP Protocol version of the http request?

(My webserver is nginx, but I guess it is irrelevant if I am using nginx or apache.)

like image 811
Pascal Klein Avatar asked Jan 11 '16 09:01

Pascal Klein


People also ask

What is difference between HTTP 1.1 and HTTP2?

As opposed to HTTP/1.1, which keeps all requests and responses in plain text format, HTTP/2 uses the binary framing layer to encapsulate all messages in binary format, while still maintaining HTTP semantics, such as verbs, methods, and headers.

How do I know if I have HTTP or HTTP2?

You can just check it in: Chrome Dev Tool ( F12 ) → Network → Protocol. It will tell you the protocol used and the domain of each transfer.

Is HTTP2 better than HTTP1?

HTTP2 is much faster and more reliable than HTTP1. HTTP1 loads a single request for every TCP connection, while HTTP2 avoids network delay by using multiplexing. HTTP is a network delay sensitive protocol in the sense that if there is less network delay, then the page loads faster.

What are two differences between HTTP and HTTP2?

These are the high-level differences between HTTP1 and HTTP2: HTTP2 is binary, instead of textual. HTTP2 is fully multiplexed, instead of ordered and blocking. HTTP2 can, therefore, use one connection for parallelism.


1 Answers

The server protocol should be available through SERVER_PROTOCOL from the server environment, usually exposed through $_SERVER['SERVER_PROTOCOL'] inside your application.

From phpinfo() under Apache 2.4:

SERVER_PROTOCOL => HTTP/1.1
like image 175
MatsLindh Avatar answered Oct 08 '22 22:10

MatsLindh