Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined index error using $_SERVER['HTTPS']

Tags:

php

Debug is throwing...

Notice: Undefined index: HTTPS in C:\xampplite\htdocs\testsite\wp-content\themes\mytheme\header.php on line 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";}
like image 762
Scott B Avatar asked Feb 06 '11 04:02

Scott B


People also ask

How do you fix an undefined index?

Undefined Index in PHP is a Notice generated by the language. The simplest way to ignore such a notice is to ask PHP to stop generating such notices. You can either add a small line of code at the top of the PHP page or edit the field error_reporting in the php. ini file.

How do you fix undefined errors?

Undefined index errors can be resolved by making use of a function called isset() function. Undefined index errors can be ignored by updating the option error_reporting to ~E_NOTICE to disable the reporting of notices.

How do you fix a undefined variable error in PHP?

Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.

What causes Undefined index in PHP?

Notice Undefined Index in PHP is an error which occurs when we try to access the value or variable which does not even exist in reality. Undefined Index is the usual error that comes up when we try to access the variable which does not persist.


1 Answers

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:

if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
    $pageURL .= "s";
}
like image 84
BoltClock Avatar answered Sep 23 '22 13:09

BoltClock