用 PHP 检测 SSL

Detecting SSL With PHP(用 PHP 检测 SSL)
本文介绍了用 PHP 检测 SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

可能的重复:
如何确定是否使用 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
}

这篇关于用 PHP 检测 SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why can#39;t I update data in an array with foreach loop?(为什么我不能用 foreach 循环更新数组中的数据?)
Foreach for arrays inside of an array(Foreach 用于数组内的数组)
PHP array get next key/value in foreach()(PHP 数组在 foreach() 中获取下一个键/值)
Using preg_match on a multidimensional array to return key values arrays(在多维数组上使用 preg_match 返回键值数组)
php foreach as key, every two number as a group(php foreach 为key,每两个数字为一组)
Treat a PHP class that implements Iterator as an array(将实现 Iterator 的 PHP 类视为数组)