How do I tell what browser is being used?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I tell what browser is being used?

Post by simonmlewis »

Code: Select all

echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
This always produces this, even in Internet Explorer 10:

[text]Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; MASBJS; rv:11.0) like Gecko [/text]
Why, and how do I show if it is IE, Firefox, Opera etc..?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I tell what browser is being used?

Post by Celauran »

I get different results for each.
Firefox: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0
Chrome: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

Code: Select all

<?php
 $browser = $_SERVER["HTTP_USER_AGENT"];
 
 echo "$browser";
 ?>
I jsut rang this in a browser.php file - away from any other code.
And I get this:
[text]Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; MASBJS; rv:11.0) like Gecko[/text]
In IE 10.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

What i've done is add it a web site that gets a lot of traffic, to see what browsers are being used, but for some reason, although varied results (in the brakcets) they all show Mozilla/5.0. SO this isn't down to my machine.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I tell what browser is being used?

Post by Celauran »

You can see my examples above also all start with Mozilla/5.0 and yet are still distinguishable.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

OH hell I see what you mean, so that's why they all start with Mozilla.
I'm trying to conjour up a code that INSERTS to be database, "Firefox", Chrome" or whatever, but I don't think that's gona be possible. It would have to be that fill string.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I tell what browser is being used?

Post by Celauran »

Firefox has 'Firefox' in the UA string. Ditto for Chrome and Safari. Check IE and Opera, I'm sure it's easy enough.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

Maybe - I guess if I do a prematch to pick out certain words, like as many browsers as I can think of, that should do it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I tell what browser is being used?

Post by Christopher »

There are a number of good libraries in PHP and Javascript that do this browser check for you. Search the web for them.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

Which part of this identifies it as Internet Explorer?

Code: Select all

Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; MASBJS; rv:11.0) like Gecko
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I tell what browser is being used?

Post by requinix »

"Trident".

"MSIE" is another keyword to search for.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I tell what browser is being used?

Post by simonmlewis »

Is there a web site/page that lists ALL these keyterms, as I'd never known Trident was Internet Explorer.
Maybe it's best I just leave my code as is.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I tell what browser is being used?

Post by Christopher »

Try get_browser()
(#10850)
Post Reply