Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined index HTTP_HOST even though it is checked

Tags:

Here's the code:

if (isset($_SERVER['HTTP_HOST']) === TRUE) {   $host = $_SERVER['HTTP_HOST']; } 

How is it possible to get an "Undefined index HTTP_HOST" on the line inside the if statement? I mean, the index setting is checked before it is used.

And why could the HTTP_HOST sometimes be not set?

like image 243
donk Avatar asked Sep 10 '12 07:09

donk


People also ask

How to Fix 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 to Fix Undefined 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.

How to Fix Undefined Index id in PHP?

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.

What is Http_host?

The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config. Which one to use depends on what you need it for.


2 Answers

Are you using PHP-CLI?

HTTP_HOST works only on the browser.

like image 172
Adil Avatar answered Sep 21 '22 21:09

Adil


A bad implemented browser can omit send que host header information, try this example:

telnet myphpserver.com 80 > GET / <enter><enter> 

in this case $_SERVER['HTTP_HOST'] not have assigned value, in this case u can use $_SERVER['SERVER_NAME'] but only if $_SERVER['HTTP_HOST'] is empty, because no is the same.

like image 34
Zeru Giran Avatar answered Sep 22 '22 21:09

Zeru Giran