WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#34353 reopened defect (bug)

redirect_canonical() – Undefined indexes 'host' and 'scheme'

Reported by: theamila Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 4.3.1
Component: Canonical Keywords: has-patch
Focuses: Cc:

Description

Hello, We have a problem on our Blog (http://blog.mila.com). Since over half a year, we get these notifications on top of our wordpress blog:

Notice: Undefined index: HTTP_HOST in /opt/wordpress/wp-includes/canonical.php on line 66 Notice: Undefined index: HTTP_HOST in /opt/wordpress/wp-includes/nav-menu-template.php on line 558

The notifications come and go, so i don't know what the problem is.. This is line 66 in the canonical file:

<?php
$requested_url .= $_SERVER['HTTP_HOST'];

And this is line 558 in nav-menu-template.php:

<?php
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );

We know that the notification is shown because the variable HTTP_HOST is not set, but we don't know how to fix that. It would be great if you could help

Attachments (2)

canonical.php (24.9 KB) - added by theamila 3 years ago.
canonical.php file
nav-menu-template.php (24.5 KB) - added by theamila 3 years ago.
nav menu template file

Download all attachments as: .zip

Change History (15)

@theamila
3 years ago

canonical.php file

@theamila
3 years ago

nav menu template file

#1 @swissspidy
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #32229. This is fixed in 4.4.

#2 @SergeyBiryukov
3 years ago

  • Focuses ui accessibility removed

#3 @theamila
3 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

I have inserted the lines from issue 32229 now i get this error:

Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 424
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 425
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 425
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 426
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 428
Notice: Undefined index: scheme in /opt/wordpress/wp-includes/canonical.php on line 445
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 424
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 425
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 425
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 426
Notice: Undefined index: host in /opt/wordpress/wp-includes/canonical.php on line 428
Notice: Undefined index: scheme in /opt/wordpress/wp-includes/canonical.php on line 445
Last edited 16 months ago by SergeyBiryukov (previous) (diff)

#4 @swissspidy
3 years ago

  • Keywords needs-patch added
  • Milestone set to Awaiting Review
  • Summary changed from Notifications on top of the page for canonical.php and nav-menu-template.php to redirect_canonical() – Undefined indexes 'host' and 'scheme'

Looks like $redirect['scheme'] and $original['host'] are not correctly set. Those two arrays are returned by parse_url, so it's certainly possible that those keys aren't set.

#5 @itproitserviceflcom
22 months ago

I have same warnings and found that it was produced by "HEAD" requests. "GET" and "POST" requests doesn't give the warning.

#6 @SergeyBiryukov
16 months ago

#39880 was marked as a duplicate.

#7 follow-up: @SergeyBiryukov
16 months ago

  • Milestone changed from Awaiting Review to 4.8

Related: #39827

This ticket was mentioned in Slack in #core by flixos90. View the logs.


13 months ago

#9 @flixos90
13 months ago

  • Milestone changed from 4.8 to Future Release

#10 @Andy Schmidt
4 months ago

This bug is still littering the log files in v4.9.4, out of the box. Spent time tracking down the cause and was going to report it, now realizing this is a known issue.

In my case, problem is that several times daily, redirect_canonical() is called with $_SERVER[HOST] empty and $_SERVER [URI] = '/'.

As a result $original[host] is never set and the code will eventually fail starting at lines 461 - 463:

<?php
        if ( strtolower($original['host']) == strtolower($redirect['host']) ||
                ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
                $redirect['host'] = $original['host'];

It should be trivial to fix at lines 70-73, currently:

<?php
        $original = @parse_url($requested_url);
        if ( false === $original ) {     // <-- this line requires a fix!
                return;
        }

by simply validating that a 'host' was actually set, since the code does expect one later:

<?php
        if ( false === $original or !isset( $original['host'] ) ) {
Last edited 4 months ago by Andy Schmidt (previous) (diff)

#11 in reply to: ↑ 7 @Andy Schmidt
4 months ago

  • Keywords has-patch added; needs-patch removed

I have confirmed that my proposed fix for 4.9.4 does address this problem. There was no recurrence in the past 4 days, and no ill effects!

Now that a confirmed fix is available, may I suggest moving it from some "future release" milestone to the next update, so that this may be closed after 2 years.

#12 @SergeyBiryukov
4 months ago

  • Milestone changed from Future Release to 5.0

#13 @archon810
3 months ago

While we're waiting for this fix, I'm also seeing a similar issue in wp-includes/class-wp-http-ixr-client.php.

ErrorException: Undefined index: host
#7 wp-includes/class-wp-http-ixr-client.php(27): handleError
#6 wp-includes/class-wp-http-ixr-client.php(27): __construct
#5 wp-includes/comment.php(2652): pingback
#4 wp-includes/comment.php(2486): do_all_pings
#3 wp-includes/class-wp-hook.php(286): apply_filters
#2 wp-includes/class-wp-hook.php(310): do_action
#1 wp-includes/plugin.php(515): do_action_ref_array
#0 wp-cron.php(126): null

Same for

Undefined index: scheme

Examining the $bits variable, I see this:

bits	
{
    path: /wordpress/xmlrpc.php
}

The function this happens in:

<?php
public function __construct($server, $path = false, $port = false, $timeout = 15) {
                if ( ! $path ) {
                        // Assume we have been given a URL instead
                        $bits = parse_url($server);
                        $this->scheme = $bits['scheme'];
                        $this->server = $bits['host'];
                        $this->port = isset($bits['port']) ? $bits['port'] : $port;
                        $this->path = !empty($bits['path']) ? $bits['path'] : '/';

All such errors are caused by wordpress/wp-cron.php?doing_wp_cron.

Perhaps the fix here can also be applied to class-wp-http-ixr-client.php?

Thank you.

Note: See TracTickets for help on using tickets.