0

I am receive the following error Notice: Undefined index: HTTP_USER_AGENT in include() but have I not defined the HTTP_USER_AGENT

function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
    elseif (strpos($user_agent, 'Edge')) return 'Edge';
    elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
    elseif (strpos($user_agent, 'Safari')) return 'Safari';
    elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
    elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Explorer';
    return 'Other';
}

Error is on this line:

<body class="<?php echo get_browser_name($_SERVER['HTTP_USER_AGENT']);?>" <?php print $attributes;?> <?php print $classes; ?>>
u_mulder
  • 54,101
  • 5
  • 48
  • 64
acctman
  • 4,229
  • 30
  • 98
  • 142
  • The code works when not used as an include? The header might not be set. – chris85 Oct 28 '17 at 16:00
  • But you are passing `$_SERVER['HTTP_USER_AGENT']` – RiggsFolly Oct 28 '17 at 16:00
  • Maybe if you showed us the COMPLETE error message and identified the line number on the error message against the code you show us, we could help – RiggsFolly Oct 28 '17 at 16:02
  • @RiggsFolly that is the complete error, the remain page is just the file location and line number. the line number points to the body class line – acctman Oct 28 '17 at 16:05
  • Exactly. So which line of the code you show us is the line mentioned in the error message please – RiggsFolly Oct 28 '17 at 16:08
  • @RiggsFolly I updated my question to show when piece of code had the error. Since the error was line 79 and non related code above it needed (a header close tag) I just posted the exact line. I apologize for that. – acctman Oct 29 '17 at 19:34

1 Answers1

-1

Reference by http://php.net/manual/en/function.get-browser.php#101125

function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
    elseif (strpos($user_agent, 'Edge')) return 'Edge';
    elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
    elseif (strpos($user_agent, 'Safari')) return 'Safari';
    elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
    elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';

    return 'Other';
}

// Usage:

echo get_browser_name($_SERVER['HTTP_USER_AGENT']);
jvk
  • 2,133
  • 3
  • 19
  • 28
  • 1
    A **good answers** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. and maybe even upvote it if it solves there issue – RiggsFolly Oct 28 '17 at 16:01
  • Now its a link only answer – RiggsFolly Oct 28 '17 at 16:03