Page 1 of 1

PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 4:07 pm
by xplore
I need to detect the users browser to apply a stylesheet whether it is internet explorer or firefox. I have searched and found some overly complicated code for what I need. Can someone help if not just show me a link to the correct function ?

Re: PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 4:38 pm
by silverspy18
Are you aware of the of the $_SERVER['HTTP_USER_AGENT'] variable (I think that's the right one, if not look here: http://us.php.net/manual/en/reserved.va ... server.php for all the $_SERVER variables.) It basically returns a string that has the user's browser and version and some other info. IE is funny when it comes to this, as I have found the version noted in the variable doesn't correctly reflect what version of IE the user is actually using. If you just need to check whether it is IE or Firefox, just search for the string 'Mozilla' within $_SERVER['HTTP_USER_AGENT'], if it's there the user is on Firefox, if not it's IE. There is also a get_browser() function available, so that might be of interest. I'm fairly new to PHP, so perhaps someone could provide more information or correct me if I'm wrong, but I figured I would just mention this much.

Re: PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 5:40 pm
by xplore
silverspy18 wrote:Are you aware of the of the $_SERVER['HTTP_USER_AGENT'] variable (I think that's the right one, if not look here: http://us.php.net/manual/en/reserved.va ... server.php for all the $_SERVER variables.) It basically returns a string that has the user's browser and version and some other info. IE is funny when it comes to this, as I have found the version noted in the variable doesn't correctly reflect what version of IE the user is actually using. If you just need to check whether it is IE or Firefox, just search for the string 'Mozilla' within $_SERVER['HTTP_USER_AGENT'], if it's there the user is on Firefox, if not it's IE. There is also a get_browser() function available, so that might be of interest. I'm fairly new to PHP, so perhaps someone could provide more information or correct me if I'm wrong, but I figured I would just mention this much.


.. how do I "search" for simply Mozilla

Re: PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 6:52 pm
by silverspy18
Use the function strpos():

Code: Select all

 
<?php
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') == false) {
        //Code to execute when the user is not using Firefox
    } else {
        //Code to execute when the user is using Firefox
    }
?>
 
I changed the code to search for 'Firefox' instead of 'Mozilla' because it wouldn't work (my only guess as to why is that since it is at the beginning of the string and strpos() is returning 0, PHP might be taking that as false, I don't know if that's true or not, plus for whatever weird reason it was in the string when I ran the script under IE.)

Basically what strpos() does is doing here is searching the string $_SERVER['HTTP_USER_AGENT'] for the string 'Firefox' and returns an integer that represents the position of the string 'Firefox'. If 'Firefox' is not found, strpos() will simply return false.

Note that if the user is using any browser other then Firefox (i.e. Opera, Safari, IE), the code directly under the if statement will run, so if you need separate code to run specifically for IE and only for IE, try searching for 'MSIE' instead.

Hope this helps.

Re: PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 11:30 pm
by McInfo
There is a non-PHP solution for this.

Code: Select all

<html>
<head>
<link rel="stylesheet" type="text/css" href="regular_stylsheet.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="ie_changes.css" />
<![endif]-->
</head>
<body>
</body>
</html>
The Internet Explorer stylesheet (ie_changes.css) would contain styles to replace the things that need to be different for IE. Other browsers ignore that stylesheet because they think it is commented out.

Edit: This post was recovered from search engine cache.

Re: PHP Code to detect users browser for simple uses

Posted: Wed Apr 15, 2009 11:55 pm
by jaoudestudios
I recommend coding for FF and then using Conditional Stylesheets like McInfo suggested. However, if you want to detect all browsers, versions and/or operating system, use a class like this.. http://www.forum.jaoudestudios.com/view ... f=13&t=106. Plus it is easily upgradable for future browser releases.

Re: PHP Code to detect users browser for simple uses

Posted: Sun Apr 19, 2009 11:33 am
by McInfo
Related article and script: http://www.sitepoint.com/article/browse ... hp-rescue/
Kevin Yank wrote:In this short article, I'll demonstrate how to replace lengthy client-side JavaScript browser detection code with a server-side equivalent written in PHP.
Note: The article was written in November 2001.

Edit: This post was recovered from search engine cache.