用 PHP 检测 SSL

2021-12-26 00:00:00 https ssl php

可能的重复:
如何确定是否使用 HTTPS$_SERVER['HTTPS']

我在网上四处寻找检测服务器是否使用 HTTPS 连接的方法,但似乎没有一个站点拥有所有的答案(以及一些不同的答案).检测服务器是否使用与 PHP 的 HTTPS 连接的所有方法究竟是什么?我需要知道几种检测 SSL 的方法,因为我的一些脚本被重新分发,并且不同的服务器处理事情的方式不同.

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.

推荐答案

$_SERVER['HTTPS']

如果脚本是通过 HTTPS 协议查询的,则设置为非空值.
注意:请注意,在 IIS 中使用 ISAPI 时,如果请求不是通过 HTTPS 协议发出的,则该值将 off.

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 off if the request was not made through the HTTPS protocol.

http://www.php.net/manual/en/reserved.variables.server.php

因此,这样做:

if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
    // SSL connection
}

相关文章