且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用 PHP 检测 SSL

更新时间:2023-02-26 13:31:20

$_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
}