Skip to content

Instantly share code, notes, and snippets.

@saippuakauppias
Last active June 8, 2020 15:38
Show Gist options
  • Save saippuakauppias/f1082a32f5797755b69b043d4852eda2 to your computer and use it in GitHub Desktop.
Save saippuakauppias/f1082a32f5797755b69b043d4852eda2 to your computer and use it in GitHub Desktop.
<?php
function is_https() {
// dont use `filter_input(INPUT_SERVER, '...')`:
// https://github.com/xwp/stream/issues/254
$https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : '';
if (!empty($https)) {
$https = strtolower($https);
if ($https == 'on') {
return true;
}
}
$proto = isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : '';
if (!empty($proto)) {
$proto = strtolower($proto);
if ($proto == 'https') {
return true;
}
}
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : '';
if ($port == 443) {
return true;
}
$front_end_https = isset($_SERVER['HTTP_FRONT_END_HTTPS']) ? $_SERVER['HTTP_FRONT_END_HTTPS'] : '';
if (!empty($front_end_https)) {
$front_end_https = strtolower($front_end_https);
if ($front_end_https == 'on') {
return true;
}
}
$cf_visitor = isset($_SERVER['HTTP_CF_VISITOR']) ? $_SERVER['HTTP_CF_VISITOR'] : null;
if (!is_null($cf_visitor)) {
try {
$cf_visitor = json_decode($cf_visitor, true);
if (is_array($cf_visitor) && array_key_exists('scheme', $cf_visitor)) {
$cf_visitor['scheme'] = strtolower($cf_visitor['scheme']);
if ($cf_visitor['scheme'] == 'https') {
return true;
}
}
} catch (Exception $e) {}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment