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?
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.
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.
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.
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.
Are you using PHP-CLI?
HTTP_HOST works only on the browser.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With