Detecting HTTPS requests in PHP

前端 未结 3 2058
醉话见心
醉话见心 2020-12-25 14:59

The problem that I am having has to do with the need to keep some urls of a website protected by HTTPS and the rest kicked to HTTP.

Normally, you have $_SERVER

相关标签:
3条回答
  • 2020-12-25 15:37

    the $_SERVER['HTTP_X_FORWARDED_PROTO'] seems to be a good solution for joomla users because if your loadbalancer does the rediretion and you set the force_ssl setting to 1 or 2 then you will end in an infinite loop because joomla always sees http:

    0 讨论(0)
  • 2020-12-25 15:38

    If the load balancer is the other end of the SSL connection, you cannot get any more info than the load balancer explicitly provides. I would go for adding a http header, it may already be doing that, dump all the HTTP headers and look.

    As another solution, you can do the redirection on the load balancer based on URL.

    0 讨论(0)
  • 2020-12-25 15:51

    If anybody has the same issue behind an Amazon AWS Elastic Load Balancer, the solution is simple because the $_SERVER variable will include:

    [HTTP_X_FORWARDED_PORT] => 443
    [HTTP_X_FORWARDED_PROTO] => https
    

    So, to get the protocol, you could use:

    function getRequestProtocol() {
        if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']))
            return $_SERVER['HTTP_X_FORWARDED_PROTO'];
        else 
            return !empty($_SERVER['HTTPS']) ? "https" : "http";
    }
    
    0 讨论(0)
提交回复
热议问题