PHP:HTTP 还是 HTTPS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4042962/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:48:00  来源:igfitidea点击:

PHP: HTTP or HTTPS?

php

提问by sachidanand

How can I tell if a php page was accessed via http or https?

如何判断 php 页面是通过 http 还是 https 访问的?

回答by Eran Galperin

If the request was sent with HTTPS you will have a extra parameter in the $_SERVERsuperglobal - $_SERVER['HTTPS']. You can check if it is set or not

如果请求是使用 HTTPS 发送的,则$_SERVER超全局变量中将有一个额外的参数- $_SERVER['HTTPS']。您可以检查它是否设置

if( isset($_SERVER['HTTPS'] ) ) {

回答by Aelios

If your request is sent by HTTPS you will have an extra server variable named 'HTTPS'

如果您的请求是通过 HTTPS 发送的,您将拥有一个名为“HTTPS”的额外服务器变量

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

回答by Mark Baijens

$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';

$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';

These should both work

这些都应该工作

回答by Bruno

This can get more complicated depending on where PHP sits in your environment, since your question is quite broad. This may depend on whether there's a load-balancer and how it's configured. Here are are a few related questions:

这可能会变得更加复杂,具体取决于 PHP 在您的环境中所处的位置,因为您的问题非常广泛。这可能取决于是否有负载平衡器及其配置方式。下面是几个相关的问题:

回答by Craige

$_SERVER['HTTPS']

This will contain a 'non-empty' value if the request was sent through HTTPS

如果请求是通过 HTTPS 发送的,这将包含一个“非空”值

PHP Server Variables

PHP 服务器变量

回答by wimvds

You should be able to do this by checking the value of $_SERVER['HTTPS'](it should only be set when using https).

您应该能够通过检查的值来做到这一点$_SERVER['HTTPS'](它应该只在使用 https 时设置)。

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

请参阅http://php.net/manual/en/reserved.variables.server.php