-1

I don't know how should I get the current request protocol in PHP. I haven't found any straight $_SERVER key for this. I don't want a straight string split out of current request.

For instance:

http://localhost/someDomain

then:

echo $_SERVER["protocl"];

// http

What should be done?

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

1 Answers1

3
$_SERVER["SERVER_PROTOCOL"];

is what you're looking for (please pay attetion, this will also return protocol version)

A workaround could be something like

$protocol_version = $_SERVER["SERVER_PROTOCOL"];
$protocol = strpos($protocol_version, 'HTTPS') === false ? 'http' : 'https';

or (more general solution)

$protocol = explode('/',$_SERVER["SERVER_PROTOCOL"])[0]; //!! valid only in php > 5.3 !!
$protocol_array = explode('/',$_SERVER["SERVER_PROTOCOL"]);
$protocol = $protocol_array[0];
DonCallisto
  • 29,419
  • 9
  • 72
  • 100