• I would like to report that version 12.3.6.3 throws a PHP notice:
    on line: 484 on File: /myurl/wp-content/plugins/wp-statistics/includes/classes/class-wp-statistics-hits.php
    The message is: Undefined index: host

Viewing 4 replies - 1 through 4 (of 4 total)
  • I could confirm this.
    Got the same message.

    I’ve investigate the source code and found the reason.

    The variable parts is created by the function parse_url (@see https://php.net/parse_url)
    by using the variable $referred wihich is filled by the method get_Referred of the class WP_Statistics which can be found in the subfolder includes/classes of the plugin.

    If the server doesn’t send a referrer the variable $referrer is false which is the data type boolean.

    To fix the issue you have to modify the code as follows:
    On lines 479 – 498 of wp-content/plugins/wp-statistics/includes/classes/class-wp-statistics-hits.php:

    From: `
    // Loop through the SE list until we find which search engine matches.
    foreach ( $search_engines as $key => $value ) {
    $search_regex = wp_statistics_searchengine_regex( $key );

    preg_match( ‘/’ . $search_regex . ‘/’, $parts[‘host’], $matches );

    if ( isset( $matches[1] ) ) {
    $data[‘last_counter’] = $WP_Statistics->Current_date( ‘Y-m-d’ );
    $data[‘engine’] = $key;
    $data[‘words’] = $WP_Statistics->Search_Engine_QueryString( $referred );
    $data[‘host’] = $parts[‘host’];
    $data[‘visitor’] = $wpdb->insert_id;

    if ( $data[‘words’] == ‘No search query found!’ ) {
    $data[‘words’] = ”;
    }

    $wpdb->insert( $wpdb->prefix . ‘statistics_search’, $data );
    }`

    To:

    					if(is_array($parts) && array_key_exists('host',$parts)){
    						// Loop through the SE list until we find which search engine matches.
    						foreach ( $search_engines as $key => $value ) {
    							$search_regex = wp_statistics_searchengine_regex( $key );
    	
    							preg_match( '/' . $search_regex . '/', $parts['host'], $matches );
    	
    							if ( isset( $matches[1] ) ) {
    								$data['last_counter'] = $WP_Statistics->Current_date( 'Y-m-d' );
    								$data['engine']       = $key;
    								$data['words']        = $WP_Statistics->Search_Engine_QueryString( $referred );
    								$data['host']         = $parts['host'];
    								$data['visitor']      = $wpdb->insert_id;
    	
    								if ( $data['words'] == 'No search query found!' ) {
    									$data['words'] = '';
    								}
    	
    								$wpdb->insert( $wpdb->prefix . 'statistics_search', $data );
    							}

    This condition will check if the variable $parts is an array and if so also check if the key ‘host’ is available. Only if both conditions success the code block will be executed.

    Thread Starter Harm10

    (@harm10)

    Thanks for confirming my report.
    You have copied the code a bit strange to make an easy comparison.
    Am I right that you have put an if around the original code?

    @harm10

    Yes, that’s exactly what I’ve done.
    Sorry I’ve marked both code examples as code, but the first isn’t recognized.
    Would be great if a preview would be available as known from other forums like stackoverflow …

    Because the value of $parts is sometimes false which is datatype boolean.
    This value is use in the preg_match as an array regardless of the datatype and therefore the notice is generated and $parts is explicitly converted to array which result in an empty array. So if ( isset( $matches[1] ) ) { ... will always be false and the code block isn’t executed.

    With the surrounding if(is_array($parts) && array_key_exists('host',$parts)){ the variable $parts is checked if it is an array and the key ‘host’ exist and only if both condition success there code block will be executed.

    Remember when updating the plugin, the changes get overridden. So keep that in mind.

    • This reply was modified 6 years, 8 months ago by mumbomedia.
    Thread Starter Harm10

    (@harm10)

    Thanks for the explanation and fix. But what I really want is the author of the plug-in to pick this up and fix it permanently.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PHP notice in version 12.3.6.3’ is closed to new replies.