-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed PHP Notice: Undefined index: HTTP_HOST #17655
Conversation
…ix-http-host-1
I have tested this item ✅ successfully on dc5cc1e This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17655. |
Is there a way to used |
I mean the notices will disappear but you generate wrong urls when the |
OS Darwin It work! |
@Murat75 can you please mark your Test at Issue Tracker |
@bembelimen Do you mean to use JUri for the link in mod_wrapper? |
@JannikMichel I think it would be a good idea to have it properly fixed in 4. |
@laoneo Ok, we will make a new PR against the 4.0-dev. |
RTC after two successful tests. @Murat75 i altered your Test. |
Would be not bad to adapt the unit tests then accordingly. |
@laoneo Going to need help in that department. |
@roland-d Ping me on glip, when you get stuck. |
@JannikMichel Yes and perhaps in the Application class, too (not sure if JUri is already available there) |
@JannikMichel Please don't forget to follow up. Thanks. |
@@ -973,6 +973,9 @@ protected function detectRequestUri() | |||
$scheme = 'http://'; | |||
} | |||
|
|||
// Try to acquire the server host, if set. | |||
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference is for
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "undefined";
I do not think leaving this as an empty value when HTTP_HOST is undefined is apropreate.
(note: I am not sure if it would cause problems with urls the set host to an undefined value)
I'm also not sure if there is a better way to handle the situation of HTTP_HOST being undefined.
(the exceptions approach for J4 is likely the direction things need to go)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about resolving to $_SERVER['SERVER_NAME']
, like here: https://github.com/slimphp/Slim/blob/3.9.0/Slim/Http/Uri.php#L176-L181
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both $_SERVER['HTTP_HOST']
and $_SERVER['SERVER_NAME']
can both be empty, then what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$_SERVER['SERVER_NAME']
would work in my case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@photodude ATM we are relying only on $_SERVER['HTTP_HOST']
so it's a step forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that adding $_SERVER['SERVER_NAME']
could be a step in the right direction. But we need to properly handle the case of both $_SERVER['HTTP_HOST']
and $_SERVER['SERVER_NAME']
being empty otherwise we are just introducing a new known problem.
We also need to check more than just Apache. What happens on Nginx or on IIS?
If we do not know the name of domain we can not create any absolute URL with schema. There is a global configuration variable joomla-cms/libraries/src/Uri/Uri.php Line 141 in 0ac5c79
and IMO it should be used if |
I checked 2 sites and error log:
access log:
|
AFAIK the |
Maybe this way?
|
I think @csthomas's suggestion is getting the closest to a proper way to address this issue. Looking at the definition of the |
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 = '';
} |
Why would you want to add Take a look at https://stackoverflow.com/questions/5705082/is-serverserver-addr-safe-to-rely-on/5705170
More info at: |
I considered 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"];
} |
@csthomas I was looking into the Not sure about behavior on Nginx or IIS. |
@photodude I would like it simple |
@Quy sounds good to me. It's much better than what's currently in place. I still question the last case. Is If possible, someone should try to put some unit tests in for this issue too. |
You shouldn't shove arbitrary data into something if you can't resolve a correct value. |
@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. |
I get this warning now and then, but not frequent enough. In my case, |
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 |
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. |
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