I've seen many tutorials online that says you need to check $_SERVER['HTTPS']
if the server is connection is secured with HTTPS. My problem is that on some of the servers I use, $_SERVER['HTTPS']
is an undefined variable that results in an error. Is there another variable I can check that should always be defined?
Just to be clear, I am currently using this code to resolve if it is an HTTPS connection:
if(isset($_SERVER['HTTPS'])) {
if ($_SERVER['HTTPS'] == "on") {
$secure_connection = true;
}
}
The only reliable method is the one described by Igor M.
Consider following: You are using nginx with fastcgi, by default(debian, ubuntu) fastgi_params contain directive:
fastcgi_param HTTPS $https;
if you are NOT using SSL, it gets translated as empty value, not 'off', not 0 and you are doomed.
http://unpec.blogspot.cz/2013/01/nette-nginx-php-fpm-redirect.html
I would add a global filter to ensure everything I am checking is correct;
Shortest way I am using:
If if https is used, then $secure_connection is true.
This also works when
$_SERVER['HTTPS']
is undefinedAs per hobodave's post: "Set to a non-empty value if the script was queried through the HTTPS protocol."
Chacha, per the PHP documentation: "Set to a non-empty value if the script was queried through the HTTPS protocol." So your if statement there will return false in many cases where HTTPS is indeed on. You'll want to verify that
$_SERVER['HTTPS']
exists and is non-empty. In cases where HTTPS is not set correctly for a given server, you can try checking if$_SERVER['SERVER_PORT'] == 443
.But note that some servers will also set
$_SERVER['HTTPS']
to a non-empty value, so be sure to check this variable also.Reference: Documentation for
$_SERVER
and$HTTP_SERVER_VARS
[deprecated]