0

Getting a PHP Warning:

Undefined array key "HTTP_USER_AGENT" in PHP 8 for the following line of code:

if ( ( strstr( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) == false ) && ( $use_object_frame == true 
) ) {

Whole section of code for this:

public function widget( $args, $instance ) {

    extract( $args );

    // Create fancy collapser
    $fancy_collapser = '';
    if ( isset( $instance['show_sliding_cart'] ) && $instance['show_sliding_cart'] == 1 ) {
        if ( isset($_SESSION['slider_state']) && is_numeric( $_SESSION['slider_state'] ) ) {
            if ( $_SESSION['slider_state'] == 0 ) {
                $collapser_image = 'plus.png';
            } else {
                $collapser_image = 'minus.png';
            }
            $fancy_collapser = ' <a href="#" onclick="return shopping_cart_collapser()" id="fancy_collapser_link"><img src="' . WPSC_CORE_IMAGES_URL . '/' . $collapser_image . '" title="" alt="" id="fancy_collapser" /></a>';
        } else {
            if ( ! wpsc_get_customer_meta( 'nzshpcart' ) ) {
                $collapser_image = 'plus.png';
            } else {
                $collapser_image = 'minus.png';
            }
            $fancy_collapser = ' <a href="#" onclick="return shopping_cart_collapser()" id="fancy_collapser_link"><img src="' . WPSC_CORE_IMAGES_URL . '/' . $collapser_image . '" title="" alt="" id="fancy_collapser" /></a>';
        }
    }

    // Start widget output
    $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Shopping Cart', 'wp-e-commerce' ) : $instance['title'] );
    echo $before_widget;

    if ( $title )
        echo $before_title . $title . $fancy_collapser . $after_title;

    // Set display state
    $display_state = '';
    if ( ( ( isset( $_SESSION['slider_state'] ) && ( $_SESSION['slider_state'] == 0 ) ) || ( wpsc_cart_item_count() < 1 ) ) && ( get_option( 'show_sliding_cart' ) == 1 ) )
        $display_state = 'style="display: none;"';

    // Output start, if we are not allowed to save results ( WPSC_DONT_CACHE ) load the cart using ajax
    $use_object_frame = false;
    if ( WPSC_PAGE_CACHE_IN_USE  ) {
        echo '<div id="sliding_cart" class="shopping-cart-wrapper">';
        if ( ( strstr( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) == false ) && ( $use_object_frame == true ) ) {
            ?>
            <object codetype="text/html" type="text/html" data="index.php?wpsc_action=cart_html_page" border="0">
                <p><?php _e( 'Loading...', 'wp-e-commerce' ); ?></p>
            </object>
            <?php
        } else {
            ?>
            <div class="wpsc_cart_loading"><p><?php _e( 'Loading...', 'wp-e-commerce' ); ?></p></div>
            <?php
        }
        echo '</div>';
    } else {
        echo '<div id="sliding_cart" class="shopping-cart-wrapper" ' . $display_state . '>';
        include( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
        echo '</div>';
    }

    // End widget output
    echo $after_widget;

}

As only at early stages of learning PHP, and have gone through some other articles on this error, still finding it difficult to resolve.

How could the line of code with the error be changed to overcome this error?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Andy
  • 23
  • 2
  • 7
  • 1
    Not all browser requests send an HTTP_USER_AGENT, and CLI requests never will. Use [`array_key_exists`](https://www.php.net/manual/en/function.array-key-exists.php) to make sure it exists before you try to use it – aynber Jan 17 '23 at 17:45
  • Or you can suppress with `$_SERVER['HTTP_USER_AGENT'] ?? '--unkown--'` – Jared Farrish Jan 17 '23 at 17:49
  • Thanks aynber. Would you know how we could add this array_key_exists into this code. Still new to PHP, so not sure how to incorporate this into the code. – Andy Jan 17 '23 at 17:49

0 Answers0