Script won't identify browser - correctly - anyone know why?

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:

Script won't identify browser - correctly - anyone know why?

Post by simonmlewis »

http://chrisschuld.com/projects/browser ... -from-php/

This page shows ways to get the browser type.

Code: Select all

$browser = $_SERVER['HTTP_USER_AGENT'];
If I do this in Firefox, it shows up as
[text]Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0[/text]

But if I do it in Internet Explorer 9, it shows up as:
[text]Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)[/text]

I need to be able to find out what the browser is, to show one of two different images (that differ because of layout differences.).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Script won't identify browser - correctly - anyone know

Post by twinedev »

Well, first what is the deciding factor that will trip a different layout? If you are needing it only for firefox:

Code: Select all

if (strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')!==FALSE) {
    // They are using firefox
} else {
    // They are using something else.
}
To change it to check for for IE, just use MSIE (uppercase) in the first line instead of Firefox

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

Re: Script won't identify browser - correctly - anyone know

Post by simonmlewis »

Perfect. Duno why mine didn't work, but I think it's that "compatible" bit of the code, and cliearly yours looks in the extract code for a particular term.

It's because IE plays with widths of divs and images differently from Firefox. And I have to get some boxes to line up with an image.

Ta!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Script won't identify browser - correctly - anyone know

Post by twinedev »

what about using conditional CSS includes?

Code: Select all

<link href="/main.css" rel="stylesheet" type="text/css">
<!--[if IE]>
    <link href="/ie-hack.css" rel="stylesheet" type="text/css">
<![endif]-->
(granted, I'm a programmer, just know that the development company I worked for used those all over to fix the way IE crapped on CSS behavior LOL)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Script won't identify browser - correctly - anyone know

Post by simonmlewis »

Already on my mind now..... :) Thx.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply