/    Sign up×
Community /Pin to ProfileBookmark

An If Statement based on Users Browser

Hi All

Is it possible to have an If statement that is assosciated with the users browser i.e.

[CODE]if (browser == IE6) { …. } elseif (browser == IE7) { … } else { …} [/CODE]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderFeb 20.2008 — [code=php]
<?php
echo $_SERVER["HTTP_USER_AGENT"];
?>
[/code]


Use the above to find the data you need mate.

It will output something like this...
<i>
</i>Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Copy linkTweet thisAlerts:
@peteybauthorFeb 20.2008 — do you know what the PHP is for:

if ($_SERVER["HTTP_USER_AGENT"] [B]contains [/B]MSIE 6.0) ....
Copy linkTweet thisAlerts:
@MrCoderFeb 20.2008 — stristr()

Or

strstr()

Or

preg_match()

I would avoid the last one unless you know how regular expressions work.

So for example..
[code=php]
if(stristr($_SERVER["HTTP_USER_AGENT"], "MSIE 6.0"))
{
// User is running MSIE 6.0
} else {
// No match
}
[/code]
Copy linkTweet thisAlerts:
@peteybauthorFeb 20.2008 — thats great, cheers matey.
Copy linkTweet thisAlerts:
@MrCoderFeb 20.2008 — Glad I could help
Copy linkTweet thisAlerts:
@felgallFeb 20.2008 — Just remember that useragent is a free format field and in at least some browsers (eg. Internet Explorer, Firefox) can be set by the browser owner to contain anything at all. Other browsers (eg. Opera) change the useragent that they pass depending on the site being visited and so report themselves as whichever browser that the site works best with.

The useragent field is therefore not suitable for any sort of processing and should only be used for stats purposes where it will give approximate results.
Copy linkTweet thisAlerts:
@peteybauthorSep 03.2008 — what would you suggest?
Copy linkTweet thisAlerts:
@NogDogSep 03.2008 — what would you suggest?[/QUOTE]
It depends on why you need to know the user's browser. If it's simply issues with making IE display correctly, you'll probably be better served by using IE conditional comments in your [X]HTML output to override the base CSS style settings to get the necessary tweaking.
Copy linkTweet thisAlerts:
@felgallSep 04.2008 — Most IE6 display issues can be fixed by making a minor change to the way you have the style coded so that the style works for IE6 and other browsers. Only in very rare cases do you need a separate style to override for IE6. Most of the IE6 bugs are well documented as to what causes the problems in the layout and what you need to do to fix it. The main problems generally relate to haslayout, the 3 pixel bug or the duplicate character bug none of which need conditional styling.
×

Success!

Help @peteyb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...