Php Get User Agent

Php

Mobile Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. The lightweight PHP class for detecting mobile devices (including tablets). Demo Download Latest version Donate. Php function getthebrowser if(strpos($SERVER'HTTPUSERAGENT', 'MSIE')! false) return 'Internet explorer'; elseif(strpos($SERVER'HTTPUSERAGENT', 'Trident')! false) return 'Internet explorer'; elseif(strpos($SERVER'HTTPUSERAGENT', 'Firefox')! false) return 'Mozilla Firefox'; elseif(strpos($SERVER'HTTPUSERAGENT', 'Chrome')! false) return 'Google Chrome'; elseif(strpos($SERVER'HTTPUSERAGENT', 'Opera Mini')! false) return 'Opera Mini'; elseif(strpos.

You should already know that you can use the $_SERVER['HTTP_USER_AGENT'] variable to see what web browser requested a page, but how can you tell whether that browser supports the HTML IFRAME element? Does that browser support CSS 2? What version number is it?

Fashion Paintings These expressive fashion paintings are perfect for anyone who has an eye for design! Providing the ideal decor for any space occupied by the fashion forward, these works of art are beautiful, colorful and modern. Enjoy high-end beauty that provides an amazing backdrop for bedrooms, dressing areas and living rooms alike. Fashion art paintings. 15,677 items - Shop our selection of Fashion canvas prints. We use premium inks for brilliant color and hand-stretch each canvas print over museum-quality stretcher bars. 60-Day Money Back Guarantee AND Free Return Shipping. Fashion is a form of escape; it lets us express ourselves and be who we want to be. We can transform our appearance just by putting on a new blouse or tie. All of your friends say you've got style and class. Now hang some art on your wall to show off your impeccable taste.

While you could glean a little more information by actually parsing the user agent, a much better solution is to let PHP do it for you! This is done using the get_browser() function, which, when given no parameters, parses the user agent from $_SERVER['HTTP_USER_AGENT']. Alternatively, you can pass it a particular user agent to work with. Either way, it returns an object that contains information about that particular web browser: what it's capable of, what version it is, and what platform it's running on.

The function works by looking up the user agent in its long list of browsers, and reading from there what that browser supports. You need to download this browser list yourself - the latest URLs are always kept in the PHP manual, but last time I checked it was http://www.garykeith.com/browsers/downloads.asp . Look in the manual for the get_browser() function, then search the page for 'browscap.ini' - the name of the browser capabilities file. Download the file (at the time of writing, the link on the PHP site takes you to another site that has a PHP-specific browscap.ini file) and place it either in c:Windows for Windows users or /etc for Unix users, then open up your php.ini file. In there, search for the line '[browscap]' and edit the next line to point to your new browscap.ini file. So, it might look like this:

Save the file and restart your web server to have the new browscap file loaded. Now, onto the code! The easiest way to get started is just to dump out all the information so you get an idea what's on offer:

That will print out the long list of information made available to you. Here are some of the things printed out for me:

[parent] => IE 6.0
[platform] => WinXP
[netclr] => 1
[browser] => IE
[version] => 6
[majorver] => 6
[minorver] => 0
[css] => 2
[frames] => 1
[iframes] => 1

From that you can see I'm running IE6 on Windows XP with .NET installed, it supports CSS v2.0, as well as HTML frames and IFRAME elements. There are quite a few other things there, too - try it yourself for more.

Now, just dumping out information like that is no use to anyone - what you really want to do is get down to the nitty-gritty of actually using that information in a smart way. For example, we could use it to load a custom CSS file for the various browsers out there. Keep in mind that each browser usually has its own way of rendering CSS styles, so having different stylesheets for each browser makes a lot of sense!

Here's some code to do just this:

User Agent Lookup

If I have to explain any of how that works, the PHP programming world is in serious trouble!

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Arbitrary-precision mathematics >>

User Agent Switcher

Previous chapter: Reflection

Jump to:

Home: Table of Contents

Php Get Os From User Agent

User

Copyright ©2015 Paul Hudson. Follow me: @twostraws.

User

Php Get User Agent Code

Follow up to Francesco R's post from 2016.
His function works for most human traffic; added a few lines to cover the most common bot traffic. Also fixed issue with function failing to detect strings at position 0 due to strpos behavior.
<?php
// Function written and tested December, 2018
function get_browser_name($user_agent)
{
// Make case insensitive.
$t = strtolower($user_agent);
// If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space.
// '[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.'
// http://php.net/manual/en/function.strpos.php
$t = ' ' . $t;
// Humans / Regular Users
if (strpos($t, 'opera' ) strpos($t, 'opr/') ) return 'Opera' ;
elseif (
strpos($t, 'edge' ) ) return 'Edge' ;
elseif (
strpos($t, 'chrome' ) ) return 'Chrome' ;
elseif (
strpos($t, 'safari' ) ) return 'Safari' ;
elseif (
strpos($t, 'firefox' ) ) return 'Firefox' ;
elseif (
strpos($t, 'msie' ) strpos($t, 'trident/7')) return 'Internet Explorer';
// Search Engines
elseif (strpos($t, 'google' ) ) return '[Bot] Googlebot' ;
elseif (
strpos($t, 'bing' ) ) return '[Bot] Bingbot' ;
elseif (
strpos($t, 'slurp' ) ) return '[Bot] Yahoo! Slurp';
elseif (
strpos($t, 'duckduckgo') ) return '[Bot] DuckDuckBot' ;
elseif (
strpos($t, 'baidu' ) ) return '[Bot] Baidu' ;
elseif (
strpos($t, 'yandex' ) ) return '[Bot] Yandex' ;
elseif (
strpos($t, 'sogou' ) ) return '[Bot] Sogou' ;
elseif (
strpos($t, 'exabot' ) ) return '[Bot] Exabot' ;
elseif (
strpos($t, 'msn' ) ) return '[Bot] MSN' ;
// Common Tools and Bots
elseif (strpos($t, 'mj12bot' ) ) return '[Bot] Majestic' ;
elseif (
strpos($t, 'ahrefs' ) ) return '[Bot] Ahrefs' ;
elseif (
strpos($t, 'semrush' ) ) return '[Bot] SEMRush' ;
elseif (
strpos($t, 'rogerbot' ) strpos($t, 'dotbot') ) return '[Bot] Moz or OpenSiteExplorer';
elseif (
strpos($t, 'frog' ) strpos($t, 'screaming')) return '[Bot] Screaming Frog';
// Miscellaneous
elseif (strpos($t, 'facebook' ) ) return '[Bot] Facebook' ;
elseif (
strpos($t, 'pinterest' ) ) return '[Bot] Pinterest' ;
// Check for strings commonly used in bot user agents
elseif (strpos($t, 'crawler' ) strpos($t, 'api' )
strpos($t, 'spider' ) strpos($t, 'http' )
strpos($t, 'bot' ) strpos($t, 'archive')
strpos($t, 'info' ) strpos($t, 'data' ) ) return '[Bot] Other' ;
return
'Other (Unknown)';
}
?>
Post with more depth here:
https://www.256kilobytes.com/content/show/1922/how-to-parse-a-user-agent-in-php-with-minimal-effort