检查服务器是否为https不工作


Check if server is https doesn't work

我试图检查服务器是否有https enabled,但它从未进入if,而是在else语句中。

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

我做错了什么?

我会回显server[https]变量。根据服务器的不同,它可能是"on"而不是"yes"

根据http://php.net/manual/en/reserved.variables.server.php

"HTTPS"通过HTTPS协议查询脚本时,不需要设置为空。

但它也添加了:

Note: Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol. 
因此,我将使用不同的逻辑:
<code>
if ( empty( $_SERVER['HTTPS'] ) ) {
    echo 'http:';
}
else if ( $_SERVER['HTTPS'] == 'off' ) {
    echo 'http:';
}
else {
    echo 'https:';
}
</code>

这样不管它是"on"还是"yes"。但是,我没有一个环境来测试https的情况。