Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is $_SERVER['REQUEST_SCHEME'] reliable?

I recently was seeking a way to properly determine protocol, under which url request was supplied to the server.

I watched through parse_url() and though $_SERVER superglobal variable, and found this:

<?php  header('Content-Type: text/plain');  print_r($_SERVER); 

Output:

[REQUEST_SCHEME] => http

However, I was unable to find it on php.net or Google. Though, I was able to find this question. Q#1: If $_SERVER['REQUEST_SCHEME'] wasn't documented, then it is probably unreliable, or it can be trusted?

I'am using VC9 PHP 5.4.14 TS under windows for development. But my production is under ubuntu. Q#2: Is this property also availible under ubuntu linux too?

like image 667
BlitZ Avatar asked Aug 02 '13 03:08

BlitZ


People also ask

What is Request_scheme?

As remarked by toxalot, REQUEST_SCHEME is a native variable of Apache web server since its version 2.4. Apache 2.2 does not have it (see Apache 2.2 server variables) and Microsoft IIs 8.5 does not have it either (see IIS 8.5 Server Variables).

What is $_ server [' Script_name ']?

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

What is $_ server [' Request_uri ']?

$_SERVER['REQUEST_URI'] contains the URI of the current page. So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials. php.

What is $_ server [' Php_self ']?

$_SERVER['PHP_SELF'] Returns the filename of the currently executing script.


1 Answers

The REQUEST_SCHEME environment variable is documented on the Apache mod_rewrite page. However, it didn't become available until Apache 2.4.

I only have Apache 2.2 so I created an environment variable. I added the following to the top of my .htaccess file.

RewriteEngine on  # Set REQUEST_SCHEME (standard environment variable in Apache 2.4) RewriteCond %{HTTPS} off RewriteRule .* - [E=REQUEST_SCHEME:http]  RewriteCond %{HTTPS} on RewriteRule .* - [E=REQUEST_SCHEME:https] 

Now I can use

  • %{ENV:REQUEST_SCHEME} in other rewrite conditions and rules
  • $_SERVER['REQUEST_SCHEME'] in my PHP code

I don't have to do extra messy conditional checks everywhere, and my PHP code is forward compatible. When Apache is upgraded, I can change my .htaccess file.

I don't know how you'd apply this to a Windows environment. This is probably not a good solution for distributed code, but it works well for my needs.

like image 63
toxalot Avatar answered Sep 24 '22 00:09

toxalot