Browse - Programming Tips - How can I get the scheme of the current script with php?

Date: 2017oct14
Language: php

Q.  How can I get the scheme of the current script with php?

A.  There is a $_SERVER['REQUEST_SCHEME'] but its not reliable so
this is a way that always works

# Helper function function safeAccess(&$myarray, $key) { if (!isset($myarray[$key])) return ''; return $myarray[$key]; } function getScheme() { $scheme = safeAccess($_SERVER, 'REQUEST_SCHEME'); $https = safeAccess($_SERVER, 'HTTPS'); $port = safeAccess($_SERVER, 'SERVER_PORT'); if ($scheme == 'https' || $https == 'on' || $port == '443') { return 'https'; } return 'http'; }