php 使用 $_SERVER['HTTPS'] 的未定义索引错误

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

Undefined index error using $_SERVER['HTTPS']

php

提问by Scott B

Debug is throwing...

调试正在抛出...

Notice: Undefined index: HTTPS in C:\xampplite\htdocs\testsite\wp-content\themes\mytheme\header.php on line 4

注意:未定义索引:HTTPS in C:\xampplite\htdocs\testsite\wp-content\themes\mytheme\header.php 第 4 行

How can I change my function below to prevent the error?

如何更改下面的函数以防止出现错误?

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 }

Would this be the equivalent?

这会是等价的吗?

if ( isset( $_SERVER["HTTPS"] )) {$pageURL .= "s";}

回答by BoltClock

Some servers simply don't set $_SERVER['HTTPS']if the request is non-secure. Some others may set it to 'off'. You'll have to check it like this:

$_SERVER['HTTPS']如果请求不安全,一些服务器根本不会设置。其他一些人可能会将其设置为'off'。你必须像这样检查它:

if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
    $pageURL .= "s";
}

回答by Blender

Notice: Undefined index: HTTPSsays that HTTPSis not a key within the $_SERVERarray. It doesn't exist, so you can't compare it with "on".

Notice: Undefined index: HTTPS说这HTTPS不是$_SERVER数组中的键。它不存在,因此您无法将其与"on".

isset()checks whether a variable is "set" or even available for reference in this case. This would be the best choice in your case.

isset()在这种情况下,检查变量是否已“设置”或什至可供参考。这将是您的最佳选择。