user5181073
user5181073

Reputation:

Check if server is https doesn't work

I'm trying to check if a server has https enabled but it never goes into the if, but in the else statement.

if ($_SERVER['HTTPS'] == "yes") {
    echo "HTTPS";
} else {
     echo "HTTP";
}

what am I doing wrong?

Upvotes: 0

Views: 195

Answers (2)

gene
gene

Reputation: 146

According to http://php.net/manual/en/reserved.variables.server.php

'HTTPS' Set to a non-empty value if the script was queried through the HTTPS protocol.

But it also adds:

Note: Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol. 

So, I would use a different logic:

<code>
if ( empty( $_SERVER['HTTPS'] ) ) {
    echo 'http:';
}
else if ( $_SERVER['HTTPS'] == 'off' ) {
    echo 'http:';
}
else {
    echo 'https:';
}
</code>

This way it does not matter if it is "on" or "yes". However, I do not have an environment to test this for the https case. http works in my case.

Upvotes: 1

klawipo
klawipo

Reputation: 153

I would echo the server[https] variable. Depending on the server it may be 'on' not 'yes'.

Upvotes: 2

Related Questions