问题
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