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

2by picture 2by · Jun 20, 2013 · Viewed 12.7k times · Source

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);
}

Answer

Daniel Paul Searles picture Daniel Paul Searles · Jun 20, 2013

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.