如何在不使用$ _SERVER ['HTTPS']的情况下查看是否使用HTTPS [英] How to find out if you're using HTTPS without $_SERVER['HTTPS']

查看:153
本文介绍了如何在不使用$ _SERVER ['HTTPS']的情况下查看是否使用HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上看到很多教程说你需要检查 $ _ SERVER ['HTTPS'] 如果服务器连接是用HTTPS保护的。我的问题是,在我使用的某些服务器上, $ _ SERVER ['HTTPS'] 是一个未定义的变量,导致错误。是否有另外一个我可以检查的变量应该始终定义?

I've seen many tutorials online that says you need to check $_SERVER['HTTPS'] if the server is connection is secured with HTTPS. My problem is that on some of the servers I use, $_SERVER['HTTPS'] is an undefined variable that results in an error. Is there another variable I can check that should always be defined?

为了清楚起见,我目前正在使用此代码来解决它是否是HTTPS连接:

Just to be clear, I am currently using this code to resolve if it is an HTTPS connection:

if(isset($_SERVER['HTTPS'])) {
    if ($_SERVER['HTTPS'] == "on") {
        $secure_connection = true;
    }
}


推荐答案

Chacha ,根据PHP文档:如果通过HTTPS协议查询脚本,则设置为非空值。因此,在确实存在HTTPS的许多情况下,你的if语句将返回false。您需要验证 $ _ SERVER ['HTTPS'] 是否存在且非空。如果没有为给定服务器正确设置HTTPS,您可以尝试检查 $ _ SERVER ['SERVER_PORT'] == 443

Chacha, per the PHP documentation: "Set to a non-empty value if the script was queried through the HTTPS protocol." So your if statement there will return false in many cases where HTTPS is indeed on. You'll want to verify that $_SERVER['HTTPS'] exists and is non-empty. In cases where HTTPS is not set correctly for a given server, you can try checking if $_SERVER['SERVER_PORT'] == 443.

但请注意,某些服务器还会将 $ _ SERVER ['HTTPS'] 设置为非空值,因此请务必检查此变量。

But note that some servers will also set $_SERVER['HTTPS'] to a non-empty value, so be sure to check this variable also.

参考:文档对于 $ _ SERVER $ HTTP_SERVER_VARS [已弃用]

Reference: Documentation for $_SERVER and $HTTP_SERVER_VARS [deprecated]

这篇关于如何在不使用$ _SERVER ['HTTPS']的情况下查看是否使用HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆