PHP:HTTP 还是 HTTPS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4042962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: HTTP or HTTPS?
提问by sachidanand
How can I tell if a php page was accessed via http or https?
如何判断 php 页面是通过 http 还是 https 访问的?
回答by Eran Galperin
回答by Aelios
If your request is sent by HTTPS you will have an extra server variable named 'HTTPS'
如果您的请求是通过 HTTPS 发送的,您将拥有一个名为“HTTPS”的额外服务器变量
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { //HTTPS }
回答by Mark Baijens
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
These should both work
这些都应该工作
回答by Bruno
This can get more complicated depending on where PHP sits in your environment, since your question is quite broad. This may depend on whether there's a load-balancer and how it's configured. Here are are a few related questions:
这可能会变得更加复杂,具体取决于 PHP 在您的环境中所处的位置,因为您的问题非常广泛。这可能取决于是否有负载平衡器及其配置方式。下面是几个相关的问题:
回答by Craige
$_SERVER['HTTPS']
This will contain a 'non-empty' value if the request was sent through HTTPS
如果请求是通过 HTTPS 发送的,这将包含一个“非空”值
回答by wimvds
You should be able to do this by checking the value of $_SERVER['HTTPS']
(it should only be set when using https).
您应该能够通过检查的值来做到这一点$_SERVER['HTTPS']
(它应该只在使用 https 时设置)。