Browser Checking

Can PHP or another programmin language check to see what browser someone is using ? For example can a check be made to see if the browser is IE 6.0, 7.0 or FireFox ?
I need to make some mods to Jinzora (an open source media player). There are some playback issues with some browsers. If I can check for a certain browser then the code will execute the code needed for the particular brwoser/player.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$browser = $_SERVER["HTTP_USER_AGENT"];

However, the user agent can be set to anything a person wants and isn't 100% reliable.

In ASP it would be browser= Request.ServerVariables("HTTP_USER_AGENT")

In any language you need to filter the user agent for malicious characters or risk XSS and SQL injections.

In any case here is a relatively up to date file of user agents to give you an example of what you are going to see in the real world.
Sorry for the disjointed answer...I keep having to answer other questions in the real world.

This should be safe in PHP.

$regExPattern = "/^[a-zA-Z0-9 \;\:\.\-\)\(\/\@\]\[\+\~\_\,\?\=\{\}\*\|\&\#\!]+$/";
preg_match($regExPattern, $_SERVER["HTTP_USER_AGENT"], $arr);
if (!empty($arr)) {
      $browser = $arr[0];
} else {
      $browser = "unknown";
echo $browser;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

mhoggatt1Author Commented:
Thanks for the replies.
I ran the code from the previous reply and
received this message when I ran it in IE 7.0 -

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)

Is this saying Mozilla is FireFox and why does it list this one first when I'm using IE 7.0 ?

Mozilla 4.0 compatible... the browser is IE7, the OS is Windows XP and it gives you the current .NET client installed.

If you download the list of user agents I pointed a link to, you'll see almost all browser user agents begin with Mozilla something.  Mozilla was the mother of all browser engines and nearly all browsers since have started their user agent string with what version of the Mozilla engine they are compatible with.

You'll also be able to use that list to translate from a long user agent to a shorter browser type.  Although it was last updated in December, so there will be a few newer user agents you'll need to add.

Mine are

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070309 Firefox/

which doesn't say FireFox until the very end.


Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)

which shows I have upgraded my .NET client since you had.


Opera/9.10 (Windows NT 5.1; U; en)

which gets to the point right away


Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20061219 Firefox/ Flock/

which is Flock not Firefox,


Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727) which is lying because it is really Maxthon which is built on top of IE.

and Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; MAXTHON 2.0)

It's all very confusing really.   Don't forget people can lie as well. Some will set their user agents to <script>alert('XSS')</script> just to see if you don't know how to protect your pages.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.