How to get rid of PHP Notice: Undefined index: HTTPS in X on line 123

雨燕双飞 提交于 2019-12-22 10:13:11

问题


I just discovered that I have thousands of these errors, coming from two of the same files.

I have removed a lot of errors by using the isset, but I can´t figure out how to remove the last two errors. Maybe you guys can help me.

PHP Notice:  Undefined index: HTTPS on /xxx/xxx/xxx.php on line 123

Code from the first PHP file that generates the error:

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

More exactly this line:

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

And

if (isset($tag)) {
$tag = htmlspecialchars($_REQUEST['tag'], ENT_QUOTES);
}

回答1:


You can change:

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

to:

if (array_key_exists('HTTPS', $_SERVER) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

array_key_exists returns a boolean. Since I check it first and use && the if statement will exit before checking the value of $_SERVER["HTTPS"] if it doesn't exist.



来源:https://stackoverflow.com/questions/17218926/how-to-get-rid-of-php-notice-undefined-index-https-in-x-on-line-123

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!