在 PHP 中检测 HTTPS 请求

发布于 2024-07-11 06:32:51 字数 389 浏览 2 评论 0原文

我遇到的问题与需要保留网站的某些网址受 HTTPS 保护而其余网址则改为 HTTP 有关。

通常,您有 $_SERVER['HTTP_HTTPS']$_SERVER['HTTPS'] (取决于您的 Apache 风格)。 您还可以检查端口 - 正常流量为 80,HTTPS 为 443。

我的问题是证书位于负载均衡器上,并且所有这些变量都不可用,并且网络服务器看到 http://www.foo.com 在端口 80 上。解决此问题的一种方法是告诉负载均衡器在不同端口上发送流量,但我想知道是否还有其他方法来检测来自负载均衡器?

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['HTTP_HTTPS'] or $_SERVER['HTTPS'] (depending on your flavor of Apache). You also can check the port - it's 80 for normal traffic and 443 for HTTPS.

My problem is that the certificate sits on the loadbalancer, and all these variables are unavailable, and the webserver sees http://www.foo.com on port 80. One way to fix this is to tell the loadbalancer to send the traffic on a different port, but I wonder if there are other ways to detect HTTPS coming from the load balancer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上青苔 2024-07-18 06:32:51

如果任何人在 Amazon AWS Elastic Load Balancer 背后遇到相同的问题,解决方案很简单,因为 $_SERVER 变量将包括:

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

因此,要获取协议,您可以使用:

function getRequestProtocol() {
    if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']))
        return $_SERVER['HTTP_X_FORWARDED_PROTO'];
    else 
        return !empty($_SERVER['HTTPS']) ? "https" : "http";
}

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";
}
滥情空心 2024-07-18 06:32:51

如果负载均衡器是 SSL 连接的另一端,则您无法获取除负载均衡器明确提供的信息之外的更多信息。 我会去添加一个 http 标头,它可能已经这样做了,转储所有 HTTP 标头并查看。

作为另一种解决方案,您可以根据 URL 在负载均衡器上进行重定向。

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.

娇俏 2024-07-18 06:32:51

$_SERVER['HTTP_X_FORWARDED_PROTO'] 对于 joomla 用户来说似乎是一个很好的解决方案,因为如果您的负载均衡器执行重定向并且您将force_ssl 设置设置为 1 或 2 那么您将以无限循环结束,因为 joomla 总是看到 http:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文