• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]

Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]

April 2, 2022 1 Comment

To get the user agent in php, you can use the $_SERVER super global variable and access the key ‘HTTP_USER_AGENT’.

$user_agent = $_SERVER['HTTP_USER_AGENT'];

The User-Agent request header is a string that lets servers and network peers identify various information like the application, operating system, vendor, and/or version of the requesting user agent.

When building web pages in php, sometimes it can be useful to know the user agent so we can provide certain information to the user.

To get the user agent in php, you can use the $_SERVER super global variable and access the key ‘HTTP_USER_AGENT’.

$user_agent = $_SERVER['HTTP_USER_AGENT'];

How to Get the Browser from the User Agent in php

The user agent variable usually is messy and doesn’t provide us a lot of value until we parse it for certain information.

One example of how to use the user agent is to determine which browser the user is accessing your web page from.

Below is a simple function (found on stack overflow), which will help you get the browser from the user agent variable in php.

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
   echo 'Internet Explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== FALSE) //For Supporting IE 11
    echo 'Internet Explorer';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE)
   echo 'Mozilla Firefox';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE)
   echo 'Google Chrome';
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== FALSE)
   echo "Opera Mini";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE)
   echo "Opera";
 elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE)
   echo "Safari";
 else
   echo 'Something else';

Hopefully this article has been useful for you to use php to get the user agent.

Other Articles You'll Also Like:

  • 1.  Remove Duplicates from Array in php with array_unique()
  • 2.  php is_numeric – Check if Variable is a Number
  • 3.  Replace Underscore with Space in php String
  • 4.  php array_merge() – Merge One or More Arrays Together by Appending
  • 5.  Get Days in Month Using php
  • 6.  Get Current Location Latitude Longitude in php
  • 7.  php atan – Find Arctangent and Inverse Tangent of Number
  • 8.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 9.  Replace Space with Dash in php String
  • 10.  php array_fill() – Create Array of Length N with Same Value

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. M.M. says

    August 1, 2022 at 5:01 am

    That function from stackoverflow is faulty and will show “Chrome” as result in many cases, since Chrome is almost always available in any user string.
    The check has to happen in the right order. less known browsers should be checked first, and Chrome and Safari should be checked at last to make sure all other browsers have been checked before.

    I find this function everywhere on the web and it’s surprising no one noticed this “bug”.

    Also, the function can be written in 3-4 lines instead of all of those if/else’s you have been using.
    Simply put the checks in a foreach loop which fetches data from your database.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy