Why isn't _SERVER[“HTTPS”] set to 1?

时光总嘲笑我的痴心妄想 提交于 2021-02-18 22:29:45

问题


My site has an SSL cert and I'm hitting https://mysite.com/info.php, but under the PHP Variables section _SERVER["HTTPS"] is not being reported. I believe this is causing a problem with a Drupal site where some URLs are being written to the page as https://... where others are being written as http://...

What determines if _SERVER["HTTPS"] is set?


EDIT: This may be the answer to my problem Detecting HTTPS vs HTTP on server sending back nothing useful. Could be a load balancer issue


回答1:


It turns out that because of the Load Balancer, which handles the SSL encryption/decryption the Web Server doesn't get $_SERVER["HTTPS"], but $_SERVER["HTTP_USESSL"] is set and can be used as a flash for SSL traffic.




回答2:


It says in the documentation $_SERVER['HTTPS']

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

so

function checkHTTPS() {
    if(!empty($_SERVER['HTTPS']))
        if($_SERVER['HTTPS'] !== 'off')
            return true; //https
        else
            return false; //http
     else
        if($_SERVER['SERVER_PORT'] == 443)
            return true; //https
        else
            return false; //http
}



回答3:


Behind a proxy / load balancer

$_SERVER['SERVER_PORT'] was always 80

$_SERVER["HTTPS"] and $_SERVER["HTTP_USESSL"] were NULL

I used $_SERVER['HTTP_X_FORWARDED_PROTO'] that return http or https




回答4:


Adding to Arnaud Hallais' post, the only way I managed to get the correct protocol on my localhost (apache/mac), testing server (apache/linux) and production site (iis/win) was with:

define("PROTOCOL", isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : ((isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) ? 'https' : 'http')); 


来源:https://stackoverflow.com/questions/11650640/why-isnt-serverhttps-set-to-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!