Dave's Brain

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 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'; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.