用 PHP 检测 SSL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7304182/
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
Detecting SSL With PHP
提问by Phillip
Possible Duplicate:
How To Find Out If You are Using HTTPS Without $_SERVER['HTTPS']
I went looking around the web for ways to detect if a server is using an HTTPS connection, but no one site seemed to have all the answers (and some different ones). What exactly are all the ways to detect if a server is using an HTTPS connection with PHP? I need to know several ways to detect SSL as some of my scripts are redistributed and various servers handle things differently.
我在网上四处寻找检测服务器是否正在使用 HTTPS 连接的方法,但似乎没有一个站点拥有所有的答案(以及一些不同的答案)。检测服务器是否使用 HTTPS 连接与 PHP 的所有方法究竟是什么?我需要知道几种检测 SSL 的方法,因为我的一些脚本被重新分发,并且不同的服务器以不同的方式处理事情。
回答by deceze
$_SERVER['HTTPS']
Set to a non-empty value if the script was queried through the HTTPS protocol.
Note: Note that when using ISAPI with IIS, the value will be offif the request was not made through the HTTPS protocol.
$_SERVER['HTTPS']
如果通过 HTTPS 协议查询脚本,则设置为非空值。
注意:请注意,将 ISAPI 与 IIS 一起使用时,如果请求不是通过 HTTPS 协议发出的,则该值将关闭。
Ergo, this'll do:
因此,这会做:
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
// SSL connection
}
回答by yoavf
WordPress's core is_ssl() function also adds a check for the server port:
WordPress 的核心is_ssl() 函数还添加了对服务器端口的检查:
function is_ssl() {
if ( isset($_SERVER['HTTPS']) ) {
if ( 'on' == strtolower($_SERVER['HTTPS']) )
return true;
if ( '1' == $_SERVER['HTTPS'] )
return true;
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
return true;
}
return false;
}