? Success
Pull Request for # 10973

User tests: Successful: Unsuccessful:

avatar JannikMichel
JannikMichel
21 Aug 2017

Pull Request for Issue #10973.

Summary of Changes

Fixed PHP Notice: Undefined index: HTTP_HOST

Testing Instructions

Connect to the Webserver via HTTP 1.0 to port 80.
Commandline:
telnet localhost 80

and in there :
get [...joomlaDirectory]/index.php HTTP/1.0

Expected result

No notices from PHP

Actual result

No notices from PHP

Documentation Changes Required

avatar joomla-cms-bot joomla-cms-bot - change - 21 Aug 2017
Category Libraries Modules Front End
avatar JannikMichel JannikMichel - open - 21 Aug 2017
avatar JannikMichel JannikMichel - change - 21 Aug 2017
Status New Pending
avatar roland-d roland-d - change - 21 Aug 2017
Rel_Number 10972
Relation Type Pull Request for
avatar roland-d roland-d - change - 21 Aug 2017
Rel_Number 10972 10973
avatar JannikMichel JannikMichel - change - 21 Aug 2017
The description was changed
avatar JannikMichel JannikMichel - edited - 21 Aug 2017
avatar JannikMichel JannikMichel - change - 21 Aug 2017
The description was changed
avatar JannikMichel JannikMichel - edited - 21 Aug 2017
avatar JannikMichel JannikMichel - change - 21 Aug 2017
The description was changed
avatar JannikMichel JannikMichel - edited - 21 Aug 2017
avatar Schmidie64 Schmidie64 - test_item - 21 Aug 2017 - Tested successfully
avatar Schmidie64
Schmidie64 - comment - 21 Aug 2017

I have tested this item successfully on dc5cc1e

@icampus
I've tested this item sucessfully. A little bit complicated but everything works well.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17655.

avatar bembelimen
bembelimen - comment - 21 Aug 2017

Is there a way to used JUri for this?

avatar laoneo
laoneo - comment - 22 Aug 2017

I mean the notices will disappear but you generate wrong urls when the HTTP_HOST variable is not set. Probably it woul be safer to throw an exception as we need th variable to generate proper urls. It can become a BC break but at least it will not generate invalid urls.

avatar Murat75
Murat75 - comment - 22 Aug 2017

OS Darwin
PHP 7.1.4
MySQLi 5.5.5-10.1.22-MariaDB

It work!
@icampus

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 22 Aug 2017

@Murat75 can you please mark your Test at Issue Tracker

avatar JannikMichel
JannikMichel - comment - 22 Aug 2017

@bembelimen Do you mean to use JUri for the link in mod_wrapper?
@laoneo I agree, when the HTTP_HOST is empty we should throw an exception. There is no use in constructing an invalid URL. This would be a BC break so I will redo this PR for the 4.0-dev branch. What do you think?

avatar laoneo
laoneo - comment - 22 Aug 2017

@JannikMichel I think it would be a good idea to have it properly fixed in 4.

avatar roland-d
roland-d - comment - 22 Aug 2017

@laoneo Ok, we will make a new PR against the 4.0-dev.

avatar franz-wohlkoenig franz-wohlkoenig - alter_testresult - 22 Aug 2017 - Murat75: Tested successfully
avatar franz-wohlkoenig franz-wohlkoenig - change - 22 Aug 2017
Status Pending Ready to Commit
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 22 Aug 2017

RTC after two successful tests.

@Murat75 i altered your Test.

avatar laoneo
laoneo - comment - 22 Aug 2017

Would be not bad to adapt the unit tests then accordingly.

avatar roland-d
roland-d - comment - 22 Aug 2017

@laoneo Going to need help in that department.

avatar laoneo
laoneo - comment - 22 Aug 2017

@roland-d Ping me on glip, when you get stuck.

avatar bembelimen
bembelimen - comment - 22 Aug 2017

@JannikMichel Yes and perhaps in the Application class, too (not sure if JUri is already available there)

avatar mbabker mbabker - change - 22 Aug 2017
Status Ready to Commit Pending
avatar roland-d
roland-d - comment - 28 Aug 2017

@JannikMichel Please don't forget to follow up. Thanks.

avatar franz-wohlkoenig franz-wohlkoenig - change - 27 Oct 2017
Status Pending Needs Review
avatar csthomas
csthomas - comment - 4 Nov 2017

HTTP_HOST is not set when we run CLI script or the Apache or NginX do not have such variable set up.

If we do not know the name of domain we can not create any absolute URL with schema.
This means that $theURI or $uri should be relative without schema and host.

There is a global configuration variable live_site, I do not know if this is deprecated.

$live_site = ($uri->isSsl()) ? str_replace('http://', 'https://', $config->get('live_site')) : $config->get('live_site');

and IMO it should be used if HTTP_HOST is not available.

avatar Quy
Quy - comment - 4 Nov 2017

I checked 2 sites and live_site is blank.
I get this error when the client is using HTTP/1.0.

error log:

PHP Notice: Undefined index: HTTP_HOST in /httpdocs/libraries/src/Application/WebApplication.php on line 988
PHP Notice: Undefined index: HTTP_HOST in /httpdocs/libraries/src/Uri/Uri.php on line 90

access log:

176.58.114.41 - - [03/Nov/2017:10:45:21 -0700] "GET / HTTP/1.0" 400 0 "-" "-"
176.58.114.41 - - [03/Nov/2017:10:45:24 -0700] "GET / HTTP/1.0" 200 75748 "-" "-"
avatar piotr-cz
piotr-cz - comment - 8 Nov 2017

AFAIK the live_site setting is meant for edge cases when it's not possible to resolve URL, like this one

avatar csthomas
csthomas - comment - 8 Nov 2017

Maybe this way?

# HTTP/1.1
if $_SERVER['HTTP_HOST'] then 
...

# HTTP/1.0 and we have server variable
elseif $_SERVER['SERVER_NAME'] then 
...

# CLI request and joomla has set up $live_site
elseif $live_site then 
...

# At least do not raise PHP notice
else 'use empty schema and host'
avatar photodude
photodude - comment - 8 Nov 2017

I think @csthomas's suggestion is getting the closest to a proper way to address this issue.
As I noted when I opened #10203 the solution to this is going to be complex.

Looking at the definition of the $live_site variable in most cases we are getting text something like http://www.yourdomainname.com where $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] would only give text like www.yourdomainname.com or yourdomainname.com
So additional processing is going to be needed to strip off the http:// from the $live_site variable

avatar photodude
photodude - comment - 8 Nov 2017

possible example based on @csthomas's suggestion

if (isset($_SERVER['HTTP_HOST']))
{
    $host =  $_SERVER['HTTP_HOST'];
}
elseif (isset($_SERVER['SERVER_NAME']))
{
    $host = $_SERVER['SERVER_NAME'];
}
elseif (!empty(JFactory::getConfig()->get('live_site')))
{
    $liveSite = str_replace("http://", "https://", JFactory::getConfig()->get('live_site'));
    $host = $liveSite;
}
elseif ( !empty($_SERVER['SERVER_ADDR']))
{
    $host = $_SERVER['SERVER_ADDR'];
}
else
{
    $host = '';
}
avatar csthomas
csthomas - comment - 9 Nov 2017

Why would you want to add$_SERVER['SERVER_ADDR']?

Take a look at https://stackoverflow.com/questions/5705082/is-serverserver-addr-safe-to-rely-on/5705170

$_SERVER['HTTP_HOST'] and $live_site - may contain schema and port, ex http://www.example.com:8080
$_SERVER['SERVER_NAME'] - usually does not contains schema or port, only www.example.com

More info at:

avatar photodude
photodude - comment - 9 Nov 2017

$_SERVER['SERVER_ADDR'] is the IP address
On IIS $_SERVER['SERVER_NAME'] might be "The server's host name, DNS alias, or IP address"

I considered $_SERVER['SERVER_ADDR'] as an alternative to possible IIS output for $_SERVER['SERVER_NAME']

based on some things I was reading for CLI we may want to also consider

elseif (isset($_ENV["HOSTNAME"]))
{
    $host = $_ENV["HOSTNAME"];
}
elseif (isset($_ENV["COMPUTERNAME"]))
{
    $host = $$_ENV["COMPUTERNAME"];
}
avatar photodude
photodude - comment - 21 Nov 2017

@csthomas I was looking into the $_SERVER['SERVER_NAME'] issue with the port. From other things I was reading with Apache 2 "if your httpd UseCanonicalName directive is set to "On" then port gets parsed out of the ServerName directive."
So $_SERVER['SERVER_NAME'] may or may not include the port depending on apache server configuration.

Not sure about behavior on Nginx or IIS.

avatar Quy
Quy - comment - 21 Nov 2017

@photodude I would like it simple HTTP_HOST > SERVER_NAME > live_site that you proposed earlier without having to consider all possible cases/setup.

avatar photodude
photodude - comment - 21 Nov 2017

@Quy sounds good to me. It's much better than what's currently in place.

I still question the last case. Is $host = ''; or $host = "undefined"; more correct. I generally am against leaving things as empty strings, but I'm not sure what the most appropriate solution here is.

If possible, someone should try to put some unit tests in for this issue too.

avatar mbabker
mbabker - comment - 21 Nov 2017

You shouldn't shove arbitrary data into something if you can't resolve a correct value.

avatar photodude
photodude - comment - 21 Nov 2017

@mbabker I somewhat agree. There is definitely a balance between not shoving data into something when a value cannot be resolved; and specifying a default/fallback value for something when a value cannot be resolved because the variable should not be empty.

This is a case where I continue to be unsure of the appropriate course of action.

avatar Quy
Quy - comment - 21 Nov 2017

I get this warning now and then, but not frequent enough. In my case, SERVER_NAME would have resolved this issue. I don't mind updating live_site as the last resort/fallback. If left empty it will be the scenario now, but at least I have control of this value if required.

avatar rotech1
rotech1 - comment - 15 Feb 2018

Joomla 5.8.5, PHP 7.1.13 on Apache still get this...

[Thu Feb 15 01:18:48 2018] [notice] [client xxx.xxx.xxx.xxx] PHP Notice: Undefined index: HTTP_HOST in [redacted]/libraries/src/Application/WebApplication.php on line 986
[Thu Feb 15 01:18:48 2018] [notice] [client xxx.xxx.xxx.xxx] PHP Notice: Undefined index: HTTP_HOST in [redacted]/libraries/src/Uri/Uri.php on line 90

avatar zero-24 zero-24 - close - 8 Aug 2021
avatar zero-24 zero-24 - change - 8 Aug 2021
Status Needs Review Closed
Closed_Date 0000-00-00 00:00:00 2021-08-08 20:08:54
Closed_By zero-24
Labels Added: ?
Removed: ?
avatar zero-24
zero-24 - comment - 8 Aug 2021

Dear @JannikMichel

in preperation of the upcomming release of Joomla 3.10 we have used GitHubs rename feature to rename the staging branch into 3.10-dev. Usually GitHub moves all existing PRs towards the new branch just fine, but here it didnt work. The reason seems to be that the fork of the CMS that was used as base for this PR has been deleted so GitHub does no longer have a base to rebase the PR against the new branch and we are also not able to reopen the PR. For that reason GitHub closed this PR in my name, when this issue is still valid It would require a new PR against the new 3.10-dev or 4.0-dev branch.

Add a Comment

Login with GitHub to post a comment